Completed
Push — master ( ae6686...307b8d )
by Tim
9s
created

ColumnComposer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 7.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 8
Bugs 0 Features 3
Metric Value
wmc 13
c 8
b 0
f 3
lcom 1
cbo 4
dl 11
loc 146
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvider() 0 4 1
A getColumnConfiguration() 0 4 1
A __construct() 11 11 1
B column() 0 28 5
A add() 0 5 1
A build() 0 10 1
A setCallableColumn() 0 10 3

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
3
namespace OpenSkill\Datatable\Composers;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\View\Factory;
7
use OpenSkill\Datatable\Columns\ColumnConfiguration;
8
use OpenSkill\Datatable\Columns\ColumnConfigurationBuilder;
9
use OpenSkill\Datatable\Columns\Orderable\Orderable;
10
use OpenSkill\Datatable\Columns\Searchable\Searchable;
11
use OpenSkill\Datatable\DatatableService;
12
use OpenSkill\Datatable\Providers\Provider;
13
use OpenSkill\Datatable\Versions\VersionEngine;
14
15
/**
16
 * Class ColumnComposer
17
 * @package OpenSkill\Datatable\Composers
18
 *
19
 * The composer is responsible to collect all column configuration as well as view configurations and to pass them
20
 * to the DTProvider when the data needs to be collected.
21
 */
22
class ColumnComposer
23
{
24
    /**
25
     * @var Provider The Provider for the underlying data.
26
     */
27
    private $provider;
28
29
    /**
30
     * @var VersionEngine The version engine that will parse the request parameter.
31
     */
32
    private $version;
33
34
    /**
35
     * @var ColumnConfiguration[] An array of the configurations of the columns
36
     */
37
    private $columnConfiguration = [];
38
39
    /**
40
     * @var Factory
41
     */
42
    private $viewFactory;
43
44
    /**
45
     * @var Repository
46
     */
47
    private $configRepository;
48
49
    /**
50
     * Will create a new datatable composer instance with the given provider
51
     * @param Provider $provider the provider that will process the underlying data
52
     * @param VersionEngine $versionEngine The version engine to handle the request data
53
     * @param Factory $viewFactory The factory providing the views
54
     * @param Repository $configRepository The repository providing the user defined options
55
     */
56 View Code Duplication
    public function __construct(
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...
57
        Provider $provider,
58
        VersionEngine $versionEngine,
59
        Factory $viewFactory,
60
        Repository $configRepository
61
    ) {
62
        $this->provider = $provider;
63
        $this->version = $versionEngine;
64
        $this->viewFactory = $viewFactory;
65
        $this->configRepository = $configRepository;
66
    }
67
68
    /**
69
     * Will return the Provider for the underlying data.
70
     * @return Provider
71
     */
72
    public function getProvider()
73
    {
74
        return $this->provider;
75
    }
76
77
    /**
78
     * Will return the internal column configurations that are registered with the current composer.
79
     *
80
     * @return ColumnConfiguration[]
81
     */
82
    public function getColumnConfiguration()
83
    {
84
        return $this->columnConfiguration;
85
    }
86
87
    /**
88
     * Will create a new ColumnConfiguration with all defaults but allows overriding of all properties through the method.
89
     *
90
     * @param string $name The name of the configuration, required for the configuration
91
     * @param string|callable $callable The function to execute, defaults to null which means the default will be set.
92
     * @param Searchable $searchable If the column should be searchable or not
93
     * @param Orderable $orderable If the column should be orderable or not
94
     * @return $this
95
     */
96
    public function column($name, $callable = null, Searchable $searchable = null, Orderable $orderable = null)
97
    {
98
        /**
99
         * @var ColumnConfigurationBuilder
100
         */
101
        $config = ColumnConfigurationBuilder::create();
102
103
        if (is_string($name)) {
104
            $config->name($name);
105
        } else {
106
            throw new \InvalidArgumentException('$name must be a string');
107
        }
108
109
        if (!is_null($callable)) {
110
            $this->setCallableColumn($config, $callable);
111
        }
112
113
        if (is_null($searchable)) {
114
            $config->searchable(Searchable::NORMAL());
115
        }
116
117
        if (is_null($orderable)) {
118
            $config->orderable(Orderable::BOTH());
119
        }
120
121
        $this->columnConfiguration[] = $config->build();
122
        return $this;
123
    }
124
125
    /**
126
     * This method will add the given ColumnConfiguration to the composer.
127
     *
128
     * @param ColumnConfiguration $configuration the configuration to add to the composer
129
     *
130
     * @return $this
131
     */
132
    public function add(ColumnConfiguration $configuration)
133
    {
134
        $this->columnConfiguration[] = $configuration;
135
        return $this;
136
    }
137
138
    /**
139
     * @return DatatableService Will return the fully built DatatableService that will contain the ColumnConfiguration
140
     */
141
    public function build()
142
    {
143
        return new DatatableService(
144
            $this->provider,
145
            $this->columnConfiguration,
146
            $this->version,
147
            $this->viewFactory,
148
            $this->configRepository
149
        );
150
    }
151
152
    /**
153
     * Determine a sane configuration value for a column's callable function
154
     * @param $config
155
     * @param $callable
156
     */
157
    private function setCallableColumn($config, $callable)
158
    {
159
        if (is_callable($callable)) {
160
            $config->withCallable($callable);
161
        } elseif (is_string($callable)) {
162
            $config->withCallable(function () use ($callable) {
163
                return $callable;
164
            });
165
        }
166
    }
167
}