AdapterFactory::forSite()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Buttress\ConcreteClient\Adapter;
3
4
use Buttress\Concrete\Locator\Site;
5
use Buttress\ConcreteClient\Exception\VersionMismatchException;
6
7
class AdapterFactory
8
{
9
10
    /**
11
     * Get an adapter from a site object
12
     *
13
     * @param \Buttress\Concrete\Locator\Site $site
14
     * @return \Buttress\ConcreteClient\Adapter\Adapter
15
     * @throws \Buttress\ConcreteClient\Exception\VersionMismatchException
16
     */
17 4
    public function forSite(Site $site)
18
    {
19 4
        return $this->forVersion($site->getVersion());
20
    }
21
22
    /**
23
     * Get an adapter for a version
24
     *
25
     * @param string $version
26
     * @return \Buttress\ConcreteClient\Adapter\Adapter
27
     * @throws \Buttress\ConcreteClient\Exception\VersionMismatchException
28
     */
29 12
    public function forVersion($version)
30
    {
31 12
        if (version_compare($version, '5.6.0', '<')) {
32 4
            throw VersionMismatchException::expected('> 5.6.0', $version);
33
        }
34
35
        $map = [
36 8
            '5.6.9999' => Version6Adapter::class,
37 4
            '5.7.9999' => Version7Adapter::class,
38
            '8.9999' => Version8Adapter::class
39 4
        ];
40
41 8
        foreach ($map as $lessThan => $class) {
42 8
            if (version_compare($version, $lessThan, '<')) {
43 8
                return new $class;
44
            }
45 4
        }
46
47
        throw new VersionMismatchException('< 9.0.0', $version);
48
    }
49
}
50