Passed
Pull Request — master (#281)
by
unknown
15:53
created

NotificationParser   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 107
Duplicated Lines 7.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 107
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 Fenos\Notifynder\Models\Notification as ModelNotification;
8
use Fenos\Notifynder\Builder\Notification as BuilderNotification;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
11
/**
12
 * Class NotificationParser.
13
 */
14
class NotificationParser
15
{
16
    /**
17
     * Regex-search-rule.
18
     */
19
    const RULE = '/\{([a-zA-Z0-9_\.]+)\}/m';
20
21
    /**
22
     * Parse a notification and return the body text.
23
     *
24
     * @param ModelNotification $notification
25
     * @param int $categoryId
26
     * @return string
27
     * @throws ExtraParamsException
28
     */
29
    public function parse($notification, $categoryId)
0 ignored issues
show
Unused Code introduced by
The parameter $categoryId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $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...
32
        if (is_null($category)) {
33
            throw (new ModelNotFoundException)->setModel(
34
                NotificationCategory::class, $notification->category_id
35
            );
36
        }
37
        $text = $category->template_body;
38
39
        $specialValues = $this->getValues($text);
40
        if (count($specialValues) > 0) {
41
            $specialValues = array_filter($specialValues, function ($value) use ($notification) {
42
                return ((is_array($notification) && isset($notification[$value])) || (is_object($notification) && isset($notification->$value))) || starts_with($value, ['extra.', 'to.', 'from.']);
43
            });
44
45
            foreach ($specialValues as $replacer) {
46
                $replace = $this->mixedGet($notification, $replacer);
47
                if (empty($replace) && notifynder_config()->isStrict()) {
48
                    throw new ExtraParamsException("The following [$replacer] param required from your category is missing.");
49
                }
50
                $text = $this->replace($text, $replace, $replacer);
51
            }
52
        }
53
54
        return $text;
55
    }
56
57
    /**
58
     * Get an array of all placehodlers.
59
     *
60
     * @param string $body
61
     * @return array
62
     */
63
    protected function getValues($body)
64
    {
65
        $values = [];
66
        preg_match_all(self::RULE, $body, $values);
67
68
        return $values[1];
69
    }
70
71
    /**
72
     * Replace a single placeholder.
73
     *
74
     * @param string $body
75
     * @param string $valueMatch
76
     * @param string $replacer
77
     * @return string
78
     */
79
    protected function replace($body, $valueMatch, $replacer)
80
    {
81
        $body = str_replace('{'.$replacer.'}', $valueMatch, $body);
82
83
        return $body;
84
    }
85
86
    /**
87
     * @param array|object $object
88
     * @param string $key
89
     * @param null|mixed $default
90
     * @return mixed
91
     */
92
    protected function mixedGet($object, $key, $default = null)
93
    {
94
        if (is_null($key) || trim($key) == '') {
95
            return '';
96
        }
97
        foreach (explode('.', $key) as $segment) {
98
            if (is_object($object) && isset($object->{$segment})) {
99
                $object = $object->{$segment};
100
                continue;
101
            }
102 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...
103
                $object = $object->__get($segment);
104
                continue;
105
            }
106 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...
107
                $object = $object->getAttribute($segment);
108
                continue;
109
            }
110
            if (is_array($object) && array_key_exists($segment, $object)) {
111
                $object = array_get($object, $segment, $default);
112
                continue;
113
            }
114
115
            return value($default);
116
        }
117
118
        return $object;
119
    }
120
}
121