VersionEngine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setDefaultVersion() 0 8 2
A setVersionFromRequest() 0 11 3
A getVersion() 0 4 1
A hasVersion() 0 4 1
A setVersion() 0 4 1
1
<?php
2
3
namespace OpenSkill\Datatable\Versions;
4
5
6
use OpenSkill\Datatable\DatatableException;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
/**
10
 * Class DTVersionEngine
11
 * @package OpenSkill\Datatable\Versions
12
 *
13
 * The DTVersionEngine will select the correct version based on the current request parameters. By default it will
14
 * support 1.9 and 1.10 of the datatable version.
15
 */
16
class VersionEngine
17
{
18
    /** @var Version The version for the request if it can be determined */
19
    private $version = null;
20
21
    /**
22
     * DTVersionEngine constructor. The first version will be set as default version.
23
     *
24
     * @param Version[] $versions an array of possible version this data table supports
25
     */
26
    public function __construct(array $versions)
27
    {
28
        $this->setVersionFromRequest($versions);
29
    }
30
31
    /**
32
     * Set the default version that will be used by the VersionEngine.
33
     *
34
     * @param Version[] $versions an array of possible version this data table supports
35
     */ 
36
    private function setDefaultVersion(array $versions)
37
    {
38
        if (count($versions) < 1) {
39
            return;
40
        }
41
42
        $this->version = $versions[0];
43
    }
44
45
    /**
46
     * Pick the verison of an engine that can parse a request.
47
     *
48
     * @param Version[] $versions an array of possible version this data table supports
49
     */
50
    private function setVersionFromRequest(array $versions)
51
    {
52
        $this->setDefaultVersion($versions);
53
54
        foreach ($versions as $v) {
55
            if ($v->canParseRequest()) {
56
                $this->version = $v;
57
                break;
58
            }
59
        }
60
    }
61
62
    /**
63
     * @return Version Will return the version that is currently selected to handle the request.
64
     */
65
    public function getVersion()
66
    {
67
        return $this->version;
68
    }
69
70
    /**
71
     * @return bool true if one of the versions can handle the request, false otherwise
72
     */
73
    public function hasVersion()
74
    {
75
        return $this->version != null;
76
    }
77
78
    /**
79
     * @param Version $version The version that should be used in this request.
80
     */
81
    public function setVersion(Version $version)
82
    {
83
        $this->version = $version;
84
    }
85
}
86