Completed
Push — master ( f4f699...429a4d )
by Tim
8s
created

DatatableService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 24.04 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 11
Bugs 0 Features 4
Metric Value
wmc 9
c 11
b 0
f 4
lcom 1
cbo 4
dl 25
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
A setVersion() 0 4 1
A shouldHandle() 0 4 2
A prepareRequest() 0 11 1
A handleRequest() 0 9 1
A view() 12 12 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;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\View\Factory;
7
use OpenSkill\Datatable\Columns\ColumnConfiguration;
8
use OpenSkill\Datatable\Providers\Provider;
9
use OpenSkill\Datatable\Versions\Version;
10
use OpenSkill\Datatable\Versions\VersionEngine;
11
use OpenSkill\Datatable\Views\DatatableView;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
15
/**
16
 * Class DatatableService
17
 * @package OpenSkill\Datatable
18
 *
19
 * The finalized and built DatatableService that can be used to handle a request or can be passed to the view.
20
 */
21
class DatatableService
22
{
23
    /** @var Provider */
24
    private $provider;
25
26
    /** @var ColumnConfiguration[] */
27
    private $columnConfigurations;
28
29
    /** @var VersionEngine */
30
    private $versionEngine;
31
32
    /** @var Factory */
33
    private $viewFactory;
34
35
    /** @var Repository */
36
    private $configRepository;
37
38
    /**
39
     * DatatableService constructor.
40
     * @param Provider $provider The provider that will prepare the data
41
     * @param ColumnConfiguration[] $columnConfigurations
42
     * @param VersionEngine $versionEngine
43
     * @param Factory $viewFactory The factory to render the views
44
     * @param Repository $configRepository The repository to get the config values from
45
     */
46 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...
47
        Provider $provider,
48
        $columnConfigurations,
49
        VersionEngine $versionEngine,
50
        Factory $viewFactory,
51
        Repository $configRepository
52
    ) {
53
        $this->provider = $provider;
54
        $this->columnConfigurations = $columnConfigurations;
55
        $this->versionEngine = $versionEngine;
56
        $this->viewFactory = $viewFactory;
57
        $this->configRepository = $configRepository;
58
    }
59
60
    /**
61
     * @param Version $version The version that should be used to generate the view and the responses
62
     */
63
    public function setVersion(Version $version)
64
    {
65
        $this->versionEngine->setVersion($version);
66
    }
67
68
    /**
69
     * @return bool True if any version should handle the current request
70
     */
71
    public function shouldHandle()
72
    {
73
        return $this->versionEngine->hasVersion() && $this->versionEngine->getVersion()->canParseRequest();
74
    }
75
76
    /**
77
     * Prepare a request for processing (without doing the actual datatable).
78
     *
79
     * @return Version
80
     */
81
    public function prepareRequest()
82
    {
83
        $version = $this->versionEngine->getVersion();
84
        $queryConfiguration = $version->parseRequest($this->columnConfigurations);
85
        $this->provider->prepareForProcessing($queryConfiguration, $this->columnConfigurations);
86
87
        return [
88
            'version' => $version,
89
            'queryConfiguration' => $queryConfiguration
90
        ];
91
    }
92
93
    /**
94
     * Will handle the current request and returns the correct response
95
     * @return JsonResponse the response that should be returned to the client.
96
     */
97
    public function handleRequest()
98
    {
99
        $request = $this->prepareRequest();
100
        $version = $request['version'];
101
        $queryConfiguration = $request['queryConfiguration'];
102
103
        $data = $this->provider->process();
104
        return $version->createResponse($data, $queryConfiguration, $this->columnConfigurations);
105
    }
106
107
    /**
108
     * @param string $tableView the view to use or null if the standard view should be used for the table and the script
109
     * @param string $scriptView the view to use or null if the standard view should be used for the table and the script
110
     * @return DatatableView
111
     */
112 View Code Duplication
    public function view($tableView = null, $scriptView = null)
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...
113
    {
114
        if (is_null($tableView)) {
115
            $tableView = $this->versionEngine->getVersion()->tableView();
116
        }
117
        if (is_null($scriptView)) {
118
            $scriptView = $this->versionEngine->getVersion()->scriptView();
119
        }
120
121
        return new DatatableView($tableView, $scriptView, $this->viewFactory, $this->configRepository,
122
            $this->columnConfigurations);
123
    }
124
}