Completed
Push — version-4 ( 9f3bb5...94cdcc )
by
unknown
02:54
created

NotificationParser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 10
wmc 8
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 21 6
A getValues() 0 6 1
A replace() 0 5 1
1
<?php
2
namespace Fenos\Notifynder\Parsers;
3
4
use Fenos\Notifynder\Exceptions\ExtraParamsException;
5
use Fenos\Notifynder\Models\Notification;
6
7
class NotificationParser
8
{
9
    const RULE = '/\{(.+?)(?:\{(.+)\})?\}/';
10
11
    public function parse(Notification $notification)
12
    {
13
        $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...
14
15
        $specialValues = $this->getValues($text);
16
        if (count($specialValues) > 0) {
17
            $specialValues = array_filter($specialValues, function ($value) use ($notification) {
18
                return isset($notification->$value) || starts_with($value, ['extra.', 'to.', 'from.']);
19
            });
20
21
            foreach ($specialValues as $replacer) {
22
                $replace = notifynder_mixed_get($notification, $replacer);
23
                if (empty($replace) && notifynder_config()->isStrict()) {
24
                    throw new ExtraParamsException("The following [$replacer] param required from your category is missing.");
25
                }
26
                $text = $this->replace($text, $replace, $replacer);
27
            }
28
        }
29
30
        return $text;
31
    }
32
33
    protected function getValues($body)
34
    {
35
        $values = [];
36
        preg_match_all(self::RULE, $body, $values);
37
        return $values[1];
38
    }
39
40
    protected function replace($body, $valueMatch, $replacer)
41
    {
42
        $body = str_replace('{'.$replacer.'}', $valueMatch, $body);
43
        return $body;
44
    }
45
}