PlacerFactory::getPlacersNames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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
use Webmozart\Assert\Assert;
14
15
class PlacerFactory
16
{
17
    const PLACER_CIRCULAR = 'circular';
18
    const PLACER_WORDLE = 'wordle';
19
    const PLACER_SPIRANGLE = 'spirangle';
20
    const PLACER_LINEAR_H = 'linear-h';
21
    const PLACER_LINEAR_V = 'linear-v';
22
    const PLACER_LISSAJOU = 'lissajou';
23
24
    /** @var PlacerFactory */
25
    protected static $instance;
26
27
    /** @var array */
28
    protected $placers = array(
29
        self::PLACER_CIRCULAR => CircularPlacer::class,
30
        self::PLACER_WORDLE => WordlePlacer::class,
31
        self::PLACER_SPIRANGLE => SpiranglePlacer::class,
32
        self::PLACER_LINEAR_H => LinearHorizontalPlacer::class,
33
        self::PLACER_LINEAR_V => LinearVerticalPlacer::class,
34
        self::PLACER_LISSAJOU => LissajouPlacer::class,
35
    );
36
37
    /**
38
     * Disallow direct instantiation
39
     */
40
    protected function __construct() { }
41
42
    /**
43
     * @return PlacerFactory
44
     */
45 6
    public  static function getInstance()
46
    {
47 6
        if (!self::$instance) {
48 1
            self::$instance = new self();
49
        }
50 6
        return self::$instance;
51
    }
52
53
    /**
54
     * @return array
55
     */
56 1
    public function getPlacersNames()
57
    {
58 1
        return array_keys($this->placers);
59
    }
60
61
    /**
62
     * @param int $imgWidth
63
     * @param int $imgHeight
64
     * @return PlacerInterface
65
     */
66 2
    public function getDefaultPlacer($imgWidth, $imgHeight)
67
    {
68 2
        return $this->getPlacer(self::PLACER_CIRCULAR, $imgWidth, $imgHeight);
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param int $imgWidth
74
     * @param int $imgHeight
75
     * @param int $increment
76
     * @return PlacerInterface
77
     */
78 6
    public function getPlacer($name, $imgWidth, $imgHeight, $increment = 10)
79
    {
80 6
        $className = $this->getPlacerClass($name);
81 6
        return new $className($imgWidth, $imgHeight, $increment);
82
    }
83
84
    /**
85
     * @param string $name
86
     * @return PlacerInterface
87
     * @throws \InvalidArgumentException
88
     */
89 6
    public function getPlacerClass($name)
90
    {
91 6
        Assert::keyExists($this->placers, $name, 'Placer not found: ' . $name);
92 6
        return $this->placers[$name];
93
    }
94
}