EmailBuilder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 110
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 4 1
A generateMergeFields() 0 3 1
A compileMergeFields() 0 6 3
A getEmailsManager() 0 3 1
A getMergeFields() 0 7 2
A setNotifiable() 0 3 1
A getNotifiable() 0 3 1
A hydrateEmail() 0 7 3
A getMergeFieldValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ByTIC\Notifications\Messages\Builder;
6
7
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...
8
use ByTIC\Notifications\Messages\Builder\ViewBuilder as GenericBuilder;
9
use ByTIC\Notifications\Notifiable;
10
use Nip\Records\AbstractModels\Record;
11
use Nip\Records\Locator\ModelLocator;
12
13
/**
14
 * Class AbstractBuilder
15
 *
16
 * @package ByTIC\Notifications\Messages\Builder
17
 */
18
abstract class EmailBuilder extends GenericBuilder
19
{
20
21
    /**
22
     * Model of notifiable trait
23
     *
24
     * @var Record|Notifiable
25
     */
26
    protected $notifiable = null;
27
28
    /**
29
     * @var array|null
30
     */
31
    protected $mergeFields = null;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function compile()
37
    {
38
        parent::compile();
39
        $this->compileMergeFields();
40
    }
41
42
    public function compileMergeFields()
43
    {
44
        $mergeFields = $this->getMergeFields();
45
        foreach ($mergeFields as $group => $fields) {
46
            foreach ($fields as $field) {
47
                $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...
48
            }
49
        }
50
    }
51
52
    /**
53
     * @return array
54
     */
55
    public function getMergeFields()
56
    {
57
        if ($this->mergeFields === null) {
58
            $this->mergeFields = $this->generateMergeFields();
59
        }
60
61
        return $this->mergeFields;
62
    }
63
64
    /**
65
     * Generates the MergeFields array
66
     *
67
     * @return array
68
     */
69
    public function generateMergeFields()
70
    {
71
        return [];
72
    }
73
74
    /** @noinspection PhpUnusedParameterInspection
75
     *
76
     * Get the MergeField value
77
     *
78
     * @param string $field Field Name
79
     *
80
     * @return null
81
     */
82
    public function getMergeFieldValue($field)
83
    {
84
        return null;
85
    }
86
87
    /**
88
     * @param EmailTrait $email
89
     * @return mixed
90
     */
91
    protected function hydrateEmail($email)
92
    {
93
        $notifiable = $this->getNotifiable();
94
        if (is_object($notifiable) && method_exists($notifiable, 'routeNotificationFor')) {
95
            $email->to = $this->getNotifiable()->routeNotificationFor('mail');
96
        }
97
        return parent::hydrateEmail($email);
98
    }
99
100
    /**
101
     * Set the Notifiable instance
102
     *
103
     * @return Record|Notifiable
104
     */
105
    public function getNotifiable()
106
    {
107
        return $this->notifiable;
108
    }
109
110
    /**
111
     * Set the Notifiable instance
112
     *
113
     * @param Record $notifiable Notifiable record
114
     *
115
     * @return void
116
     */
117
    public function setNotifiable($notifiable)
118
    {
119
        $this->notifiable = $notifiable;
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    protected function getEmailsManager()
126
    {
127
        return ModelLocator::get('emails');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Nip\Records\Locat...lLocator::get('emails') returns the type Nip\Records\AbstractModels\RecordManager which is incompatible with the return type mandated by ByTIC\Notifications\Mess...der::getEmailsManager() of Nip\MailModule\Models\EmailsTable\EmailsTrait.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
128
    }
129
}
130