Completed
Push — master ( f41bd3...e94ae8 )
by Tim
05:26
created

VersionEngine   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 2
Metric Value
wmc 9
c 8
b 0
f 2
lcom 1
cbo 1
dl 0
loc 60
rs 10

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
    private function setDefaultVersion(array $versions)
32
    {
33
        if (count($versions) < 1) {
34
            throw new DatatableException('VersionEngine needs at least one engine (default)');
35
        }
36
37
        $this->version = $versions[0];
38
    }
39
40
    private function setVersionFromRequest(array $versions)
41
    {
42
        $this->setDefaultVersion($versions);
43
44
        foreach ($versions as $v) {
45
            if ($v->canParseRequest()) {
46
                $this->version = $v;
47
                break;
48
            }
49
        }
50
    }
51
52
    /**
53
     * @return Version Will return the version that is currently selected to handle the request.
54
     */
55
    public function getVersion()
56
    {
57
        return $this->version;
58
    }
59
60
    /**
61
     * @return bool true if one of the versions can handle the request, false otherwise
62
     */
63
    public function hasVersion()
64
    {
65
        return $this->version != null;
66
    }
67
68
    /**
69
     * @param Version $version The version that should be used in this request.
70
     */
71
    public function setVersion(Version $version)
72
    {
73
        $this->version = $version;
74
    }
75
}