Passed
Push — master ( 1877a1...37d1bb )
by Gabriel
10:54
created

EmailBuilder::generateMergeFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Notifications\Messages\Builder;
4
5
use ByTIC\Common\Records\Emails\EmailTrait;
0 ignored issues
show
Bug introduced by
The type ByTIC\Common\Records\Emails\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...
6
use ByTIC\Common\Records\Emails\Builder\ViewBuilder as GenericBuilder;
0 ignored issues
show
Bug introduced by
The type ByTIC\Common\Records\Emails\Builder\ViewBuilder 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...
7
use ByTIC\Notifications\Notifications\AbstractNotification as Notification;
8
use ByTIC\Notifications\Notifiable;
9
use Nip\Records\AbstractModels\Record;
10
use Nip\Records\Locator\ModelLocator;
11
12
/**
13
 * Class AbstractBuilder
14
 *
15
 * @package ByTIC\Notifications\Messages\Builder
16
 */
17
abstract class EmailBuilder extends GenericBuilder
18
{
19
20
    /**
21
     * Model of notifiable trait
22
     *
23
     * @var Record|Notifiable
24
     */
25
    protected $notifiable = null;
26
27
    /**
28
     * @var array|null
29
     */
30
    protected $mergeFields = null;
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function compile()
36
    {
37
        parent::compile();
38
        $this->compileMergeFields();
39
    }
40
41
    public function compileMergeFields()
42
    {
43
        $mergeFields = $this->getMergeFields();
44
        foreach ($mergeFields as $group => $fields) {
45
            foreach ($fields as $field) {
46
                $this->setMergeTag($field, $this->getMergeFieldValue($field));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getMergeFieldValue($field) targeting ByTIC\Notifications\Mess...r::getMergeFieldValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
            }
48
        }
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getMergeFields()
55
    {
56
        if ($this->mergeFields === null) {
57
            $this->mergeFields = $this->generateMergeFields();
58
        }
59
60
        return $this->mergeFields;
61
    }
62
63
    /**
64
     * Generates the MergeFields array
65
     *
66
     * @return array
67
     */
68
    public function generateMergeFields()
69
    {
70
        return [];
71
    }
72
73
    /** @noinspection PhpUnusedParameterInspection
74
     *
75
     * Get the MergeField value
76
     *
77
     * @param string $field Field Name
78
     *
79
     * @return null
80
     */
81
    public function getMergeFieldValue($field)
82
    {
83
        return null;
84
    }
85
86
    /**
87
     * @param EmailTrait $email
88
     * @return mixed
89
     */
90
    protected function hydrateEmail($email)
91
    {
92
        $notifiable = $this->getNotifiable();
93
        if (method_exists($notifiable, 'routeNotificationFor')) {
94
            $email->to = $this->getNotifiable()->routeNotificationFor('mail');
95
        }
96
        return parent::hydrateEmail($email);
97
    }
98
99
    /**
100
     * Set the Notifiable instance
101
     *
102
     * @return Record|Notifiable
103
     */
104
    public function getNotifiable()
105
    {
106
        return $this->notifiable;
107
    }
108
109
    /**
110
     * Set the Notifiable instance
111
     *
112
     * @param Record $notifiable Notifiable record
113
     *
114
     * @return void
115
     */
116
    public function setNotifiable($notifiable)
117
    {
118
        $this->notifiable = $notifiable;
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    protected function getEmailsManager()
125
    {
126
        return ModelLocator::get('emails');
127
    }
128
}
129