Completed
Push — master ( 590d86...13b77a )
by Bukashk0zzz
02:10
created

LiipImagineSerializableField::checkArrayOption()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 7
Ratio 58.33 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 12
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzLiipImagineSerializationBundle
5
 *
6
 * (c) Denis Golubovskiy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bukashk0zzz\LiipImagineSerializationBundle\Annotation;
13
14
use Doctrine\ORM\Mapping\Annotation;
15
16
/**
17
 * LiipImagineSerializableField
18
 *
19
 * @Annotation
20
 * @Target({"PROPERTY", "METHOD"})
21
 *
22
 * @author Denis Golubovskiy <[email protected]>
23
 */
24
final class LiipImagineSerializableField implements Annotation
25
{
26
    /**
27
     * @var string $filter LiipImagine Filter
28
     */
29
    private $filter;
30
31
    /**
32
     * @var string $vichUploaderField Field
33
     */
34
    private $vichUploaderField;
35
36
    /**
37
     * @var string $virtualField Virtual Field
38
     */
39
    private $virtualField;
40
41
    /**
42
     * @var array $options Options
43
     */
44
    private $options;
45
46
47
    /**
48
     * Constructor
49
     *
50
     * @param array $options Options
51
     *
52
     * @throws \Exception
53
     */
54
    public function __construct(array $options)
55
    {
56
        $this->options = $options;
57
58
        if (!array_key_exists('value', $this->options) && !array_key_exists('filter', $this->options)) {
59
            throw new \LogicException(sprintf('Either "value" or "filter" option must be set.'));
60
        }
61
62
        if ($this->checkArrayOption('value')) {
63
            $this->setFilter($options['value']);
64
        } elseif ($this->checkArrayOption('filter')) {
65
            $this->setFilter($this->options['filter']);
66
        }
67
68
        if ($this->checkStringOption('vichUploaderField')) {
69
            $this->setVichUploaderField($this->options['vichUploaderField']);
70
        }
71
72
        if ($this->checkStringOption('virtualField')) {
73
            $this->setVirtualField($this->options['virtualField']);
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getFilter()
81
    {
82
        return $this->filter;
83
    }
84
85
    /**
86
     * @param string $filter
87
     * @return $this
88
     */
89
    public function setFilter($filter)
90
    {
91
        $this->filter = $filter;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getVichUploaderField()
100
    {
101
        return $this->vichUploaderField;
102
    }
103
104
    /**
105
     * @param string $vichUploaderField
106
     * @return $this
107
     */
108
    public function setVichUploaderField($vichUploaderField)
109
    {
110
        $this->vichUploaderField = $vichUploaderField;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getVirtualField()
119
    {
120
        return $this->virtualField;
121
    }
122
123
    /**
124
     * @param string $virtualField
125
     * @return $this
126
     */
127
    public function setVirtualField($virtualField)
128
    {
129
        $this->virtualField = $virtualField;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param $optionName
136
     * @return bool
137
     * @throws \Exception
138
     */
139
    private function checkStringOption($optionName)
140
    {
141 View Code Duplication
        if (array_key_exists($optionName, $this->options)) {
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...
142
            if (!is_string($this->options[$optionName])) {
143
                throw new \InvalidArgumentException(sprintf('Option "'.$optionName.'" must be a string.'));
144
            }
145
146
            return true;
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @param $optionName
154
     * @return bool
155
     * @throws \Exception
156
     */
157
    private function checkArrayOption($optionName)
158
    {
159 View Code Duplication
        if (array_key_exists($optionName, $this->options)) {
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...
160
            if (!is_array($this->options[$optionName]) && !is_string($this->options[$optionName])) {
161
                throw new \InvalidArgumentException(sprintf('Option "'.$optionName.'" must be a array or string.'));
162
            }
163
164
            return true;
165
        }
166
167
        return false;
168
    }
169
}
170