PdfLettersTrait::getStatsManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Models\Downloads\DownloadsTrait;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\Stats\StatsTrait;
7
use ByTIC\MediaLibrary\HasMedia\HasMediaRecordsTrait;
8
use Nip\Records\AbstractModels\RecordManager;
9
use Nip\Records\Traits\AbstractTrait\RecordsTrait as AbstractRecordsTrait;
10
11
/**
12
 * Class PdfLettersTrait
13
 * @package ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters
14
 *
15
 * @method PdfLetterTrait[] findByParams($params)
16
 */
17
trait PdfLettersTrait
18
{
19
    use AbstractRecordsTrait;
20
    use HasMediaRecordsTrait;
21
22
    /**
23
     * @param string $type
24
     * @param int $idItem
25
     * @return bool|PdfLetterTrait
26
     */
27
    public function getByItem($type, $idItem)
28
    {
29
        /** @var PdfLetterTrait[] $letters */
30
        $letters = $this->findByParams(
31
            [
32
                'where' => [
33
                    ['id_item = ?', $idItem],
34
                    ['type = ?', $type],
35
                ],
36
                'order' => [['id', 'DESC']],
37
            ]
38
        );
39
40
        $return = false;
41
42
        if (count($letters)) {
43
            foreach ($letters as $letter) {
44
                if ($letter->hasFile() && !$return) {
45
                    $return = $letter;
46
                } else {
47
//                    $item->delete();
48
                }
49
            }
50
        }
51
52
        return $return;
53
    }
54
55
    /**
56
     * @return DownloadsTrait|RecordManager
57
     */
58
    public function getDownloadsManager()
59
    {
60
        return $this->getRelation('Downloads')->getWith();
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

60
        return $this->/** @scrutinizer ignore-call */ getRelation('Downloads')->getWith();
Loading history...
61
    }
62
63
    /**
64
     * @return StatsTrait|RecordManager
65
     */
66
    public function getStatsManager()
67
    {
68
        return $this->getRelation('Stats')->getWith();
69
    }
70
71
    /**
72
     * @param $type
73
     * @return AbstractRecordsTrait
74
     */
75
    abstract public function getParentManagerFromType($type);
76
77
    protected function initRelations()
78
    {
79
        /** @noinspection PhpUndefinedClassInspection */
80
        parent::initRelations();
81
        $this->initRelationsTrait();
82
    }
83
84
    protected function initRelationsTrait()
85
    {
86
        $this->initCustomFieldsRelation();
87
        $this->initDownloadsRelation();
88
        $this->initStatsRelation();
89
    }
90
91
    protected function initCustomFieldsRelation()
92
    {
93
        $this->hasMany('CustomFields', $this->getCustomFieldsRelationParams());
94
    }
95
96
    protected function initDownloadsRelation()
97
    {
98
        if (method_exists($this, 'getDownloadsManagerClass')) {
99
            $this->hasMany('Downloads', [
100
                'class' => $this->getDownloadsManagerClass(),
101
                'fk' => $this->getPrimaryFK(),
102
            ]);
103
        }
104
    }
105
106
    protected function initStatsRelation()
107
    {
108
        if (method_exists($this, 'getStatsManagerClass')) {
109
            $this->hasMany('Stats', [
110
                'class' => $this->getStatsManagerClass(),
111
                'fk' => $this->getPrimaryFK(),
112
            ]);
113
        }
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    protected function getCustomFieldsRelationParams()
120
    {
121
        return [
122
            'class' => $this->getCustomFieldsManagerClass(),
123
            'fk' => $this->getPrimaryFK(),
124
        ];
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    abstract protected function getCustomFieldsManagerClass();
131
132
//    /**
133
//     * @return string
134
//     */
135
//    abstract protected function getDownloadsManagerClass();
136
137
//    /**
138
//     * @return string
139
//     */
140
//    abstract protected function getStatsManagerClass();
141
142
    /**
143
     * @return string
144
     */
145
    abstract public function getPrimaryFK();
146
}
147