Completed
Pull Request — master (#282)
by Tom
15:11
created

NotificationParser   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 106
Duplicated Lines 7.55 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 106
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
D parse() 0 27 10
A getValues() 0 7 1
A replace() 0 6 1
C mixedGet() 8 28 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Fenos\Notifynder\Parsers;
4
5
use Fenos\Notifynder\Models\NotificationCategory;
6
use Fenos\Notifynder\Exceptions\ExtraParamsException;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Fenos\Notifynder\Models\Notification as ModelNotification;
9
10
/**
11
 * Class NotificationParser.
12
 */
13
class NotificationParser
14
{
15
    /**
16
     * Regex-search-rule.
17
     */
18
    const RULE = '/\{([a-zA-Z0-9_\.]+)\}/m';
19
20
    /**
21
     * Parse a notification and return the body text.
22
     *
23
     * @param ModelNotification $notification
24
     * @return string
25
     * @throws ExtraParamsException
26
     */
27
    public function parse($notification)
28
    {
29
        $category = $notification->category;
0 ignored issues
show
Bug introduced by
The property category does not seem to exist. Did you mean category_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
        if (is_null($category)) {
31
            throw (new ModelNotFoundException)->setModel(
32
                NotificationCategory::class, $notification->category_id
33
            );
34
        }
35
        $text = $category->template_body;
36
37
        $specialValues = $this->getValues($text);
38
        if (count($specialValues) > 0) {
39
            $specialValues = array_filter($specialValues, function ($value) use ($notification) {
40
                return ((is_array($notification) && isset($notification[$value])) || (is_object($notification) && isset($notification->$value))) || starts_with($value, ['extra.', 'to.', 'from.']);
41
            });
42
43
            foreach ($specialValues as $replacer) {
44
                $replace = $this->mixedGet($notification, $replacer);
45
                if (empty($replace) && notifynder_config()->isStrict()) {
46
                    throw new ExtraParamsException("The following [$replacer] param required from your category is missing.");
47
                }
48
                $text = $this->replace($text, $replace, $replacer);
49
            }
50
        }
51
52
        return $text;
53
    }
54
55
    /**
56
     * Get an array of all placehodlers.
57
     *
58
     * @param string $body
59
     * @return array
60
     */
61
    protected function getValues($body)
62
    {
63
        $values = [];
64
        preg_match_all(self::RULE, $body, $values);
65
66
        return $values[1];
67
    }
68
69
    /**
70
     * Replace a single placeholder.
71
     *
72
     * @param string $body
73
     * @param string $valueMatch
74
     * @param string $replacer
75
     * @return string
76
     */
77
    protected function replace($body, $valueMatch, $replacer)
78
    {
79
        $body = str_replace('{'.$replacer.'}', $valueMatch, $body);
80
81
        return $body;
82
    }
83
84
    /**
85
     * @param array|object $object
86
     * @param string $key
87
     * @param null|mixed $default
88
     * @return mixed
89
     */
90
    protected function mixedGet($object, $key, $default = null)
91
    {
92
        if (is_null($key) || trim($key) == '') {
93
            return '';
94
        }
95
        foreach (explode('.', $key) as $segment) {
96
            if (is_object($object) && isset($object->{$segment})) {
97
                $object = $object->{$segment};
98
                continue;
99
            }
100 View Code Duplication
            if (is_object($object) && method_exists($object, '__get') && ! is_null($object->__get($segment))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
                $object = $object->__get($segment);
102
                continue;
103
            }
104 View Code Duplication
            if (is_object($object) && method_exists($object, 'getAttribute') && ! is_null($object->getAttribute($segment))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
                $object = $object->getAttribute($segment);
106
                continue;
107
            }
108
            if (is_array($object) && array_key_exists($segment, $object)) {
109
                $object = array_get($object, $segment, $default);
110
                continue;
111
            }
112
113
            return value($default);
114
        }
115
116
        return $object;
117
    }
118
}
119