Completed
Push — master ( d56194...c8d3eb )
by Oleg
11:26
created

LinkFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 26.56 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 4
dl 17
loc 64
ccs 28
cts 28
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A build() 0 7 1
A setDisplayRule() 0 6 2
A serialize() 9 9 2
A unserialize() 8 8 2

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
namespace Malezha\Menu\Factory;
3
4
use Illuminate\Contracts\Container\Container;
5
use Malezha\Menu\Contracts\Attributes;
6
use Malezha\Menu\Element\Link;
7
use Opis\Closure\SerializableClosure;
8
9
/**
10
 * Class LinkFactory
11
 * @package Malezha\Menu\Factory
12
 *
13
 * @property string $title
14
 * @property string $url
15
 * @property Attributes $attributes
16
 * @property Attributes $activeAttributes
17
 * @property Attributes $linkAttributes
18
 * @property string $view
19
 * @property mixed $displayRule
20
 *
21
 */
22
class LinkFactory extends AbstractElementFactory
23
{
24
    /**
25
     * @inheritdoc
26
     */
27 24
    public function __construct(Container $container)
28
    {
29 24
        parent::__construct($container);
30
        
31 24
        $this->parameters = [
32 24
            'title' => '',
33 24
            'url' => '#',
34 24
            'attributes' => $this->app->make(Attributes::class, ['attributes' => []]),
35 24
            'activeAttributes' => $this->app->make(Attributes::class, ['attributes' => []]),
36 24
            'linkAttributes' => $this->app->make(Attributes::class, ['attributes' => []]),
37 24
            'view' => $this->getElementConfig(Link::class)['view'],
38
            'displayRule' => true,
39
        ];
40 24
    }
41
42
    /**
43
     * @param array $parameters
44
     * @return Link
45
     */
46 14
    public function build($parameters = [])
47
    {
48 14
        $link = $this->app->make(Link::class, $this->mergeParameters($parameters));
49 14
        $this->setDisplayRule($link);
50
        
51 14
        return $link;
52
    }
53
    
54 14
    protected function setDisplayRule(Link $link)
55
    {
56 14
        if (array_key_exists('displayRule', $this->parameters)) {
57 14
            $link->setDisplayRule($this->parameters['displayRule']);
58
        }
59 14
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1 View Code Duplication
    public function serialize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66 1
        $params = $this->parameters;
67 1
        if ($params['displayRule'] instanceof \Closure) {
68 1
            $params['displayRule'] = new SerializableClosure($params['displayRule']);
69
        }
70
71 1
        return serialize($params);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 1 View Code Duplication
    public function unserialize($serialized)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79 1
        parent::unserialize($serialized);
80
81 1
        if ($this->parameters['displayRule'] instanceof SerializableClosure) {
82 1
            $this->parameters['displayRule'] = $this->parameters['displayRule']->getClosure();
83
        }
84
    }
85
}