DownloadStatsProcess::processForManager()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.9666
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Actions;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Models\Downloads\DownloadsTrait;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters\PdfLettersTrait;
7
use ByTIC\DocumentGenerator\PdfLetters\PdfLettersManager;
8
use Nip\MailModule\Models\EmailsTable\EmailsTrait;
0 ignored issues
show
Bug introduced by
The type Nip\MailModule\Models\EmailsTable\EmailsTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Nip\Records\AbstractModels\RecordManager as AbstractRecordManager;
10
use Nip\Records\RecordManager;
11
use Nip\Records\Locator\ModelLocator;
12
13
/**
14
 * Class DownloadStatsProcess
15
 * @package ByTIC\DocumentGenerator\PdfLetters\Actions
16
 */
17
class DownloadStatsProcess
18
{
19
    /**
20
     * @var PdfLettersManager[]
21
     */
22
    protected $managers = [];
23
24
    /**
25
     * DownloadStatsProcess constructor.
26
     * @param array $managers
27
     */
28
    public function __construct(array $managers = null)
29
    {
30
        $this->managers = is_array($managers) ? $managers : app('dg.pdf-letters.manager')->getManagerClasses();
31
    }
32
33
    public function handle()
34
    {
35
        foreach ($this->managers as $manager) {
36
            $manager = is_object($manager) ? $manager : ModelLocator::get($manager);
37
            $this->processForManager($manager);
0 ignored issues
show
Bug introduced by
It seems like $manager can also be of type ByTIC\DocumentGenerator\...tters\PdfLettersManager; however, parameter $letterManager of ByTIC\DocumentGenerator\...ss::processForManager() does only seem to accept Nip\Records\AbstractModels\RecordManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            $this->processForManager(/** @scrutinizer ignore-type */ $manager);
Loading history...
38
        }
39
    }
40
41
    /**
42
     * @param RecordManager|AbstractRecordManager|PdfLettersTrait $letterManager
43
     */
44
    protected function processForManager(AbstractRecordManager $letterManager)
45
    {
46
        /** @noinspection PhpUnhandledExceptionInspection */
47
        if (!$letterManager->hasRelation('Downloads')) {
48
            return;
49
        }
50
        $downloadsManager = $letterManager->getDownloadsManager();
51
        $letters = $this->getLastLettersFromDownloads($letterManager, $downloadsManager);
0 ignored issues
show
Bug introduced by
It seems like $downloadsManager can also be of type true; however, parameter $downloadsManager of ByTIC\DocumentGenerator\...tLettersFromDownloads() does only seem to accept ByTIC\DocumentGenerator\...p\Records\RecordManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $letters = $this->getLastLettersFromDownloads($letterManager, /** @scrutinizer ignore-type */ $downloadsManager);
Loading history...
52
53
        $statsManager = $letterManager->getStatsManager();
54
        foreach ($letters as $letter) {
55
            if ($downloadsManager->shouldTrackLetter($letter)) {
0 ignored issues
show
Bug introduced by
The method shouldTrackLetter() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            if ($downloadsManager->/** @scrutinizer ignore-call */ shouldTrackLetter($letter)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
                continue;
57
            }
58
            $statsManager->compileForLetter($letter);
0 ignored issues
show
Bug introduced by
The method compileForLetter() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            $statsManager->/** @scrutinizer ignore-call */ 
59
                           compileForLetter($letter);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
        }
60
    }
61
62
    /**
63
     * @param PdfLettersTrait|RecordManager $letterManager
64
     * @param DownloadsTrait|RecordManager $downloadsManager
65
     */
66
    protected function getLastLettersFromDownloads($letterManager, $downloadsManager)
67
    {
68
        /** @noinspection PhpUnhandledExceptionInspection */
69
        $letterPK = $letterManager->getRelation('Downloads')->getFK();
0 ignored issues
show
Bug introduced by
It seems like getRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
        $letterPK = $letterManager->/** @scrutinizer ignore-call */ getRelation('Downloads')->getFK();
Loading history...
70
71
        $queryDownloads = $downloadsManager->newSelectQuery();
0 ignored issues
show
Bug introduced by
It seems like newSelectQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        /** @scrutinizer ignore-call */ 
72
        $queryDownloads = $downloadsManager->newSelectQuery();
Loading history...
72
        $queryDownloads->setCols($letterPK);
0 ignored issues
show
Bug introduced by
$letterPK of type string is incompatible with the type array expected by parameter $| of Nip\Database\Query\AbstractQuery::setCols(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        $queryDownloads->setCols(/** @scrutinizer ignore-type */ $letterPK);
Loading history...
73
        $queryDownloads->group($letterPK);
0 ignored issues
show
Bug introduced by
$letterPK of type string is incompatible with the type array expected by parameter $fields of Nip\Database\Query\Select::group(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $queryDownloads->group(/** @scrutinizer ignore-type */ $letterPK);
Loading history...
74
75
        $queryLetters = $letterManager->newSelectQuery();
76
        $queryLetters->where('id IN ?', $queryDownloads);
0 ignored issues
show
Bug introduced by
$queryDownloads of type Nip\Database\Query\Select is incompatible with the type array expected by parameter $values of Nip\Database\Query\AbstractQuery::where(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $queryLetters->where('id IN ?', /** @scrutinizer ignore-type */ $queryDownloads);
Loading history...
77
        $queryLetters->limit(10);
78
79
        return $letterManager->findByQuery($queryLetters);
0 ignored issues
show
Bug introduced by
The method findByQuery() does not exist on ByTIC\DocumentGenerator\...Letters\PdfLettersTrait. Did you maybe mean findOneByQuery()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        return $letterManager->/** @scrutinizer ignore-call */ findByQuery($queryLetters);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
    }
81
}
82