Passed
Push — master ( 90b3af...835a3e )
by Gabriel
14:13
created

FieldsTrait::bootFieldsTrait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\DocumentGenerator\PdfLetters\Models\Fields;
4
5
use ByTIC\DocumentGenerator\PdfLetters\Models\Fields\Types\AbstractType;
6
use ByTIC\DocumentGenerator\PdfLetters\Models\PdfLetters\PdfLettersTrait;
7
use Nip\MailModule\Models\EmailsTable\EmailTrait;
0 ignored issues
show
Bug introduced by
The type Nip\MailModule\Models\EmailsTable\EmailTrait 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...
8
use Nip\Records\EventManager\Events\Event;
9
use Nip\Records\Traits\AbstractTrait\RecordsTrait as AbstractRecordsTrait;
10
use ByTIC\Models\SmartProperties\RecordsTraits\HasTypes\RecordsTrait as HasTypeRecordsTrait;
11
12
/**
13
 * Class FieldsTrait
14
 * @package ByTIC\DocumentGenerator\PdfLetters\Models\Fields
15
 */
16
trait FieldsTrait
17
{
18
    use AbstractRecordsTrait;
19
    use HasTypeRecordsTrait;
20
21
    /**
22
     * @var null|array
23
     */
24
    protected $mergeFields = null;
25
    /**
26
     * @var null
27
     */
28
    protected $mergeFieldsType = null;
29
30
31
    public function bootFieldsTrait()
32
    {
33
        static::creating(function (Event $event) {
34
35
            /** @var FieldTrait|\Nip\Records\Record $record */
36
            $record = $event->getRecord();
37
38
            $record->setIf('metadata', '{}', function () use ($record) {
39
                return count($record->metadata) < 1;
0 ignored issues
show
Bug introduced by
It seems like $record->metadata can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, 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

39
                return count(/** @scrutinizer ignore-type */ $record->metadata) < 1;
Loading history...
40
            });
41
        });
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function getMergeFields()
48
    {
49
        if ($this->mergeFields === null) {
50
            $this->initMergeFields();
51
        }
52
53
        return $this->mergeFields;
54
    }
55
56
    /**
57
     * @param AbstractType $type
58
     */
59
    public function populateTagsFromType($type)
60
    {
61
        $typeTags = (array)$type->providesTags();
62
        foreach ($typeTags as $tag) {
63
            $this->mergeFields[$type->getCategory()][] = $tag;
64
            $this->mergeFieldsType[$tag] = $type->getName();
65
        }
66
    }
67
68
    /**
69
     * @param $tag
70
     * @return null|string
71
     */
72
    public function getFieldTypeFromMergeTag($tag)
73
    {
74
        if ($this->mergeFieldsType === null) {
75
            $this->initMergeFields();
76
        }
77
        if (isset($this->mergeFieldsType[$tag])) {
78
            return $this->mergeFieldsType[$tag];
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * @param array $params
86
     */
87
    public function injectParams(&$params = [])
88
    {
89
        /** @noinspection PhpUndefinedClassInspection */
90
        parent::injectParams($params);
91
        $params['order'][] = ['Y', 'ASC'];
92
        $params['order'][] = ['X', 'ASC', false];
93
    }
94
95
    /**
96
     * @return PdfLettersTrait
97
     */
98
    abstract public function getLetterManager();
99
100
    protected function initMergeFields()
101
    {
102
        /** @var AbstractType[] $types */
103
        $types = $this->getTypes();
104
        $this->mergeFields = [];
105
        foreach ($types as $type) {
106
            $this->populateTagsFromType($type);
107
        }
108
    }
109
110
    protected function registerSmartPropertyType()
111
    {
112
        $this->registerSmartProperty('field', 'Type');
113
    }
114
}
115