ColumnConfigurationBuilder   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 152
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 4 1
A name() 0 5 1
A searchable() 0 5 1
A orderable() 0 5 1
A withCallable() 0 5 1
A build() 0 9 1
A checkName() 0 6 2
A checkOrderable() 0 6 2
A checkSearchable() 0 6 2
C checkCallable() 0 22 9
1
<?php
2
3
namespace OpenSkill\Datatable\Columns;
4
5
use OpenSkill\Datatable\Columns\Orderable\Orderable;
6
use OpenSkill\Datatable\Columns\Searchable\Searchable;
7
8
/**
9
 * Class ColumnConfigurationBuilder
10
 * @package OpenSkill\Datatable\Columns
11
 *
12
 * Simple builder class for the column configuration
13
 */
14
class ColumnConfigurationBuilder
15
{
16
17
    /**
18
     * @var string
19
     */
20
    private $name = null;
21
22
    /**
23
     * @var callable
24
     */
25
    private $callable = null;
26
27
    /**
28
     * @var Searchable
29
     */
30
    private $searchable = null;
31
32
    /**
33
     * @var Orderable
34
     */
35
    private $orderable = null;
36
37
    /**
38
     * ColumnConfigurationBuilder constructor.
39
     */
40
    private function __construct()
41
    {
42
    }
43
44
    /**
45
     * Will create a new builder for a ColumnConfigurationBuilder.
46
     *
47
     * @return ColumnConfigurationBuilder
48
     */
49
    public static function create()
50
    {
51
        return new ColumnConfigurationBuilder();
52
    }
53
54
    /**
55
     * @param string $name
56
     * @return $this
57
     */
58
    public function name($name)
59
    {
60
        $this->name = $name;
61
        return $this;
62
    }
63
64
    /**
65
     * @param Searchable $searchable
66
     * @return $this
67
     */
68
    public function searchable(Searchable $searchable)
69
    {
70
        $this->searchable = $searchable;
71
        return $this;
72
    }
73
74
    /**
75
     * @param Orderable $orderable
76
     * @return $this
77
     */
78
    public function orderable(Orderable $orderable)
79
    {
80
        $this->orderable = $orderable;
81
        return $this;
82
    }
83
84
    /**
85
     * @param callable $callable
86
     * @return $this
87
     */
88
    public function withCallable($callable)
89
    {
90
        $this->callable = $callable;
91
        return $this;
92
    }
93
94
    /**
95
     * Will create the final ColumnConfiguration
96
     *
97
     * @return ColumnConfiguration
98
     */
99
    public function build()
100
    {
101
        $this->checkName();
102
        $this->checkCallable();
103
        $this->checkOrderable();
104
        $this->checkSearchable();
105
106
        return new ColumnConfiguration($this->name, $this->callable, $this->searchable, $this->orderable);
107
    }
108
109
    /**
110
     * Will check if the name is empty and throws an exception if that is the case.
111
     */
112
    private function checkName()
113
    {
114
        if (empty($this->name)) {
115
            throw new \InvalidArgumentException("The name can not be empty");
116
        }
117
    }
118
119
    /**
120
     * Will check if the orderable flag is correctly set, otherwise it will be set to the default NONE
121
     */
122
    private function checkOrderable()
123
    {
124
        if ($this->orderable == null) {
125
            $this->orderable = Orderable::BOTH();
126
        }
127
    }
128
129
    /**
130
     * Will check if the searchable flag is correctly set, if not it will be set to the default NONE
131
     */
132
    private function checkSearchable()
133
    {
134
        if ($this->searchable == null) {
135
            $this->searchable = Searchable::NORMAL();
136
        }
137
    }
138
139
    /**
140
     * Will check if the callable is set and is executable, if not a sensible default will be set.
141
     */
142
    private function checkCallable()
143
    {
144
        if (is_null($this->callable) || !is_callable($this->callable)) {
145
            $this->callable = function ($data) {
146
                $name = $this->name;
147
148
                if (is_array($data) && array_key_exists($name, $data)) {
149
                    return $data[$name];
150
                }
151
152
                if (is_object($data) && property_exists($data, $name)) {
153
                    return $data->$name;
154
                }
155
156
                if (is_object($data) && method_exists($data, $name)) {
157
                    return $data->$name();
158
                }
159
160
                return "";
161
            };
162
        }
163
    }
164
165
}