Completed
Push — version-4 ( a26cca...5903bf )
by
unknown
02:38
created

NotificationParser::parse()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 12
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 21
rs 8.7624
1
<?php
2
3
namespace Fenos\Notifynder\Parsers;
4
5
use Fenos\Notifynder\Exceptions\ExtraParamsException;
6
use Fenos\Notifynder\Models\Notification;
7
8
class NotificationParser
9
{
10
    const RULE = '/\{(.+?)(?:\{(.+)\})?\}/';
11
12
    public function parse(Notification $notification)
13
    {
14
        $text = $notification->template_body;
0 ignored issues
show
Documentation introduced by
The property template_body does not exist on object<Fenos\Notifynder\Models\Notification>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
15
16
        $specialValues = $this->getValues($text);
17
        if (count($specialValues) > 0) {
18
            $specialValues = array_filter($specialValues, function ($value) use ($notification) {
19
                return isset($notification->$value) || starts_with($value, ['extra.', 'to.', 'from.']);
20
            });
21
22
            foreach ($specialValues as $replacer) {
23
                $replace = notifynder_mixed_get($notification, $replacer);
24
                if (empty($replace) && notifynder_config()->isStrict()) {
25
                    throw new ExtraParamsException("The following [$replacer] param required from your category is missing.");
26
                }
27
                $text = $this->replace($text, $replace, $replacer);
28
            }
29
        }
30
31
        return $text;
32
    }
33
34
    protected function getValues($body)
35
    {
36
        $values = [];
37
        preg_match_all(self::RULE, $body, $values);
38
39
        return $values[1];
40
    }
41
42
    protected function replace($body, $valueMatch, $replacer)
43
    {
44
        $body = str_replace('{'.$replacer.'}', $valueMatch, $body);
45
46
        return $body;
47
    }
48
}
49