Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
CRAP
93.10% covered (success)
93.10%
27 / 29
DataPercentSerie
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
9.03
93.10% covered (success)
93.10%
27 / 29
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
 collect
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
8 / 8
 getDataSerie
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getTotalPercentDataSerie
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 validate
0.00% covered (danger)
0.00%
0 / 1
5.27
77.78% covered (warning)
77.78%
7 / 9
<?php
namespace Alxvng\QATracker\DataProvider\Model;
use Alxvng\QATracker\DataProvider\Finder\ProviderFinder;
use DateTime;
use JsonException;
use Symfony\Component\String\Slugger\AsciiSlugger;
use function Symfony\Component\String\u;
class DataPercentSerie extends AbstractDataSerie
{
    protected AbstractDataSerie $dataSerie;
    protected AbstractDataSerie $totalPercentDataSerie;
    /**
     * DataProvider constructor.
     *
     * @param array  $config
     * @param string $generatedDir
     * @param array  $dataSeriesStack
     *
     * @throws JsonException
     */
    public function __construct(array $config, string $generatedDir, array $dataSeriesStack)
    {
        $slugger = new AsciiSlugger();
        $this->slug = u($slugger->slug($config['id']))->lower();
        $storageDir = $generatedDir.'/'.static::PROVIDERS_DIR;
        $this->storageFilePath = $storageDir.'/'.$this->getSlug().'.json';
        $this->id = $config['id'];
        $this->dataSerie = ProviderFinder::findById($config['provider'], $dataSeriesStack);
        $this->totalPercentDataSerie = ProviderFinder::findById($config['totalPercentProvider'], $dataSeriesStack);
        $this->validate();
        $this->load();
    }
    /**
     * @param DateTime $trackDate
     *
     * @throws JsonException
     */
    public function collect(DateTime $trackDate): void
    {
        $dataSerie = $this->getDataSerie();
        $totalPercentProvider = $this->getTotalPercentDataSerie();
        $value = $dataSerie->getInstance()->fetchData();
        $total = $totalPercentProvider->getInstance()->fetchData();
        $percent = $value * 100 / $total;
        $this->addData($percent, $trackDate);
        $this->save();
    }
    public function getDataSerie(): AbstractDataSerie
    {
        return $this->dataSerie;
    }
    public function getTotalPercentDataSerie(): AbstractDataSerie
    {
        return $this->totalPercentDataSerie;
    }
    protected function validate(): void
    {
        if (!$this->dataSerie instanceof DataStandardSerie) {
            throw new \RuntimeException(sprintf('Unable to collect data. %s expect data serie, got %s', DataStandardSerie::class, get_class($this->dataSerie)));
        }
        if ($this->dataSerie->getId() === $this->getId()) {
            throw new \RuntimeException('You can\'t refer the data serie itself.');
        }
        if (!$this->totalPercentDataSerie instanceof DataStandardSerie) {
            throw new \RuntimeException(sprintf('Unable to collect data. %s expect data serie, got %s', DataStandardSerie::class, get_class($this->totalPercentDataSerie)));
        }
        if ($this->totalPercentDataSerie->getId() === $this->getId()) {
            throw new \RuntimeException('You can\'t refer the data serie itself.');
        }
    }
}