Completed
Push — master ( 49c7a9...f71cbe )
by Dan
02:30
created

PlacerFactory::getPlacerClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace SixtyNine\Cloud\Factory;
4
5
6
use SixtyNine\Cloud\Placer\CircularPlacer;
7
use SixtyNine\Cloud\Placer\LinearHorizontalPlacer;
8
use SixtyNine\Cloud\Placer\LinearVerticalPlacer;
9
use SixtyNine\Cloud\Placer\LissajouPlacer;
10
use SixtyNine\Cloud\Placer\PlacerInterface;
11
use SixtyNine\Cloud\Placer\SpiranglePlacer;
12
use SixtyNine\Cloud\Placer\WordlePlacer;
13
14
class PlacerFactory
15
{
16
    /** @var PlacerFactory */
17
    protected static $instance;
18
19
    /** @var array */
20
    protected $placers = array(
21
        'Circular' => CircularPlacer::class,
22
        'Wordle' => WordlePlacer::class,
23
        'Spirangle' => SpiranglePlacer::class,
24
        'Linear Horizontal' => LinearHorizontalPlacer::class,
25
        'Linear Vertical' => LinearVerticalPlacer::class,
26
        'Lissajou' => LissajouPlacer::class,
27
    );
28
29
    protected function __construct() { }
30
31 4
    public  static function getInstance()
32
    {
33 4
        if (!self::$instance) {
34 1
            self::$instance = new self();
35
        }
36 4
        return self::$instance;
37
    }
38
39 1
    public function getPlacersNames()
40
    {
41 1
        return array_keys($this->placers);
42
    }
43
44 2
    public function getDefaultPlacer($imgWidth, $imgHeight)
45
    {
46 2
        return $this->getPlacer('Circular', $imgWidth, $imgHeight);
47
    }
48
49 4
    public function getPlacer($name, $imgWidth, $imgHeight, $increment = 10)
50
    {
51 4
        $className = $this->getPlacerClass($name);
52 4
        return new $className($imgWidth, $imgHeight, $increment);
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return PlacerInterface
58
     * @throws \InvalidArgumentException
59
     */
60 4
    protected function getPlacerClass($name)
61
    {
62 4
        if (!array_key_exists($name, $this->placers)) {
63
            throw new \InvalidArgumentException('Placer not found: ' . $name);
64
        }
65
66 4
        return $this->placers[$name];
67
    }
68
}