CacheTagsNormalizer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 8
eloc 12
c 2
b 2
f 0
dl 0
loc 37
ccs 10
cts 14
cp 0.7143
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 8 3
A checkTagForString() 0 5 3
A errorOut() 0 4 2
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class CacheTagsNormalizer implements NormalizerContract
8
{
9
    /**
10
     * ّFigures out what the cache tags should be.
11
     *
12
     * @param  object  $widget
13
     * @return void
14
     */
15 24
    public function normalize($widget): void
16
    {
17 24
        if (! property_exists($widget, 'cacheTags')) {
18 23
            $widget->cacheTags = [];
19 1
        } elseif (is_array($widget->cacheTags)) {
20 1
            $this->checkTagForString($widget);
21
        } else {
22
            $this->errorOut($widget);
23
        }
24 24
    }
25
26
    /**
27
     * @param $widget
28
     * @param  null  $tag
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tag is correct as it would always require null to be passed?
Loading history...
29
     */
30
    private function errorOut($widget, $tag = null): void
31
    {
32
        $tag = $tag ? ' '.$tag.'is not string' : '';
0 ignored issues
show
introduced by
$tag is of type null, thus it always evaluated to false.
Loading history...
33
        throw new \InvalidArgumentException('Cache Tags on "'.get_class($widget).'" must be of type Array with String elements.'.$tag);
34
    }
35
36
    /**
37
     * @param $widget
38
     */
39 1
    protected function checkTagForString($widget): void
40
    {
41 1
        foreach ($widget->cacheTags as $tag) {
42 1
            if (! is_string($tag)) {
43 1
                $this->errorOut($widget);
44
            }
45
        }
46 1
    }
47
}
48