NotificationParser::mixedGet()   C
last analyzed

Complexity

Conditions 14
Paths 7

Size

Total Lines 28
Code Lines 18

Duplication

Lines 8
Ratio 28.57 %

Importance

Changes 0
Metric Value
cc 14
eloc 18
nc 7
nop 3
dl 8
loc 28
rs 5.0864
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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