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

TextFactory::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
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\Text;
7
use Opis\Closure\SerializableClosure;
8
9
/**
10
 * Class TextFactory
11
 * @package Malezha\Menu\Factory
12
 * 
13
 * @property string $text
14
 * @property Attributes $attributes
15
 * @property mixed $displayRule
16
 */
17
class TextFactory extends AbstractElementFactory
18
{
19
    /**
20
     * @param array $parameters
21
     * @return Text
22
     */
23 7
    public function build($parameters = [])
24
    {
25 7
        $text = $this->app->make(Text::class, $this->mergeParameters($parameters));
26 7
        if (array_key_exists('displayRule', $this->parameters)) {
27 7
            $text->setDisplayRule($this->parameters['displayRule']);
28
        }
29
30 7
        return $text;
31
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 10
    public function __construct(Container $container)
37
    {
38 10
        parent::__construct($container);
39
        
40 10
        $this->parameters = [
41 10
            'text' => null,
42
            'displayRule' => true,
43 10
            'attributes' => $this->app->make(Attributes::class, ['attributes' => []]),
44 10
            'view' => $this->getElementConfig(Text::class)['view'],
45
        ];
46 10
    }
47
48
49
    /**
50
     * @inheritDoc
51
     */
52 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...
53
    {
54 1
        $params = $this->parameters;
55 1
        if ($params['displayRule'] instanceof \Closure) {
56
            $params['displayRule'] = new SerializableClosure($params['displayRule']);
57
        }
58
59 1
        return serialize($params);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 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...
66
    {
67 1
        parent::unserialize($serialized);
68
69 1
        if ($this->parameters['displayRule'] instanceof SerializableClosure) {
70
            $this->parameters['displayRule'] = $this->parameters['displayRule']->getClosure();
71
        }
72
    }
73
}