Completed
Push — version-4 ( 75ffe7...58de13 )
by
unknown
06:48
created

NotificationParser   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 3
Metric Value
dl 0
loc 93
rs 10
c 4
b 1
f 3
wmc 22
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 21 6
A getValues() 0 7 1
A replace() 0 6 1
C mixedGet() 0 21 14
1
<?php
2
3
namespace Fenos\Notifynder\Parsers;
4
5
use Fenos\Notifynder\Exceptions\ExtraParamsException;
6
use Fenos\Notifynder\Models\Notification;
7
8
/**
9
 * Class NotificationParser.
10
 */
11
class NotificationParser
12
{
13
    /**
14
     * Regex-search-rule.
15
     */
16
    const RULE = '/\{([a-zA-Z0-9_\.]+)\}/m';
17
18
    /**
19
     * Parse a notification and return the body text.
20
     *
21
     * @param Notification $notification
22
     * @return string
23
     * @throws ExtraParamsException
24
     */
25
    public function parse(Notification $notification)
26
    {
27
        $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...
28
29
        $specialValues = $this->getValues($text);
30
        if (count($specialValues) > 0) {
31
            $specialValues = array_filter($specialValues, function ($value) use ($notification) {
32
                return isset($notification->$value) || starts_with($value, ['extra.', 'to.', 'from.']);
33
            });
34
35
            foreach ($specialValues as $replacer) {
36
                $replace = $this->mixedGet($notification, $replacer);
37
                if (empty($replace) && notifynder_config()->isStrict()) {
38
                    throw new ExtraParamsException("The following [$replacer] param required from your category is missing.");
39
                }
40
                $text = $this->replace($text, $replace, $replacer);
41
            }
42
        }
43
44
        return $text;
45
    }
46
47
    /**
48
     * Get an array of all placehodlers.
49
     *
50
     * @param string $body
51
     * @return array
52
     */
53
    protected function getValues($body)
54
    {
55
        $values = [];
56
        preg_match_all(self::RULE, $body, $values);
57
58
        return $values[1];
59
    }
60
61
    /**
62
     * Replace a single placeholder.
63
     *
64
     * @param string $body
65
     * @param string $valueMatch
66
     * @param string $replacer
67
     * @return string
68
     */
69
    protected function replace($body, $valueMatch, $replacer)
70
    {
71
        $body = str_replace('{'.$replacer.'}', $valueMatch, $body);
72
73
        return $body;
74
    }
75
76
    /**
77
     * @param array|object $object
78
     * @param string $key
79
     * @param null|mixed $default
80
     * @return mixed
81
     */
82
    protected function mixedGet($object, $key, $default = null)
83
    {
84
        if (is_null($key) || trim($key) == '') {
85
            return '';
86
        }
87
        foreach (explode('.', $key) as $segment) {
88
            if (is_object($object) && isset($object->{$segment})) {
89
                $object = $object->{$segment};
90
            } elseif (is_object($object) && method_exists($object, '__get') && ! is_null($object->__get($segment))) {
91
                $object = $object->__get($segment);
92
            } elseif (is_object($object) && method_exists($object, 'getAttribute') && ! is_null($object->getAttribute($segment))) {
93
                $object = $object->getAttribute($segment);
94
            } elseif (is_array($object) && array_key_exists($segment, $object)) {
95
                $object = array_get($object, $segment, $default);
96
            } else {
97
                return value($default);
98
            }
99
        }
100
101
        return $object;
102
    }
103
}
104