Passed
Push — master ( 7822c6...0f91e9 )
by Sebastian
03:25
created

TrackingIDTrait::getTrackingIDToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Traits\Commands\Validation\TrackingIDTrait} trait.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\TrackingIDTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Commands\Command\ShowURL\AutoTrackingID;
15
use Mailcode\Interfaces\Commands\TrackableInterface;
16
use Mailcode\Interfaces\Commands\Validation\TrackingIDInterface;
17
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
18
use Mailcode\Mailcode_Parser_Statement_Validator;
19
use phpDocumentor\Descriptor\Interfaces\FunctionInterface;
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Descriptor...faces\FunctionInterface 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...
20
21
/**
22
 * Command validation drop-in: checks for the presence
23
 * of a tracking ID, which must be the first string
24
 * literal in the command's parameters list. If not
25
 * present or not a match for a tracking ID name, an
26
 * empty string is used as default.
27
 *
28
 * When the `no-tracking:` keyword is enabled, the
29
 * tracking ID will always be empty.
30
 *
31
 * @package Mailcode
32
 * @subpackage Validation
33
 * @author Sebastian Mordziol <[email protected]>
34
 *
35
 * @see TrackingIDInterface
36
 */
37
trait TrackingIDTrait
38
{
39
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $trackingIDToken = null;
40
41
    /**
42
     * @return string
43
     */
44
    public function getTrackingID() : string
45
    {
46
        $token = $this->getTrackingIDToken();
47
48
        if($token === null)
49
        {
50
            return '';
51
        }
52
53
        $trackingID = $token->getText();
54
55
        if(empty($trackingID))
56
        {
57
            $trackingID = AutoTrackingID::generate($this);
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands...idation\TrackingIDTrait is incompatible with the type Mailcode\Interfaces\Comm...ion\TrackingIDInterface expected by parameter $command of Mailcode\Commands\Comman...oTrackingID::generate(). ( Ignorable by Annotation )

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

57
            $trackingID = AutoTrackingID::generate(/** @scrutinizer ignore-type */ $this);
Loading history...
58
            $token->setText($trackingID);
59
        }
60
61
        return $token->getText();
62
    }
63
64
    public function getTrackingIDToken() : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
65
    {
66
        $this->initTrackingToken();
67
68
        return $this->trackingIDToken;
69
    }
70
71
    private function initTrackingToken() : void
72
    {
73
        if(!$this->isTrackingEnabled())
0 ignored issues
show
Bug introduced by
It seems like isTrackingEnabled() 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

73
        if(!$this->/** @scrutinizer ignore-call */ isTrackingEnabled())
Loading history...
74
        {
75
            $this->clearTrackingToken();
76
            return;
77
        }
78
79
        if(isset($this->trackingIDToken))
80
        {
81
            return;
82
        }
83
84
        $token = $this->detectToken();
85
        if($token === null)
86
        {
87
            $token = $this->requireParams()
0 ignored issues
show
Bug introduced by
It seems like requireParams() 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

87
            $token = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
88
                ->getInfo()
89
                ->addStringLiteral(AutoTrackingID::generate($this));
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands...idation\TrackingIDTrait is incompatible with the type Mailcode\Interfaces\Comm...ion\TrackingIDInterface expected by parameter $command of Mailcode\Commands\Comman...oTrackingID::generate(). ( Ignorable by Annotation )

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

89
                ->addStringLiteral(AutoTrackingID::generate(/** @scrutinizer ignore-type */ $this));
Loading history...
90
        }
91
92
        $this->trackingIDToken = $token;
93
    }
94
95
    private function clearTrackingToken() : void
96
    {
97
        $this->trackingIDToken = null;
98
99
        $token = $this->detectToken();
100
        if($token !== null)
101
        {
102
            $this->requireParams()
103
                ->getInfo()
104
                ->removeToken($token);
105
        }
106
    }
107
108
    public function setTrackingID(string $trackingID) : self
109
    {
110
        $token = $this->getTrackingIDToken();
111
112
        if($token !== null)
113
        {
114
            $token->setText($trackingID);
115
        }
116
117
        return $this;
118
    }
119
120
    public function hasTrackingID() : bool
121
    {
122
        $token = $this->getTrackingIDToken();
123
124
        return $token !== null && !empty($token->getText());
125
    }
126
127
    private function detectToken() : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
128
    {
129
        $literals = $this->requireParams()
130
            ->getInfo()
131
            ->getStringLiterals();
132
133
        if(empty($literals))
134
        {
135
            return null;
136
        }
137
138
        $trackingID = array_shift($literals);
139
140
        $id = $trackingID->getText();
141
142
        if(strpos($id, '=') === false)
143
        {
144
            return $trackingID;
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * Checks if any of the parameters contain a trackingID.
152
     * This must be the first string literal in the parameters,
153
     * allowing any keywords to be placed before it, but not
154
     * after the optional query parameters.
155
     */
156
    protected function validateSyntax_tracking_id() : void
157
    {
158
        $this->initTrackingToken();
159
    }
160
}
161