Completed
Push — master ( fa81e5...c1047a )
by De Cramer
04:54 queued 01:25
created

CompatibleFetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace eXpansion\Framework\Core\Helpers;
5
use eXpansion\Bundle\Maps\Model\Map;
6
use oliverde8\AssociativeArraySimplified\AssociativeArray;
7
8
9
/**
10
 * Class CompatibleFetcher
11
 *
12
 * @package eXpansion\Framework\Core\Helpers;
13
 * @author  oliver de Cramer <[email protected]>
14
 */
15
class CompatibleFetcher
16
{
17
    /** For compatibility with every title/mode/script */
18
    const COMPATIBLE_ALL = "ALL";
19
20
    /**
21
     * Constant used for unknown titles.
22
     */
23
    const GAME_UNKNOWN = 'unknown';
24
25
    /** @var AssociativeArray  */
26
    protected $titles;
27
28
    /**
29
     * TitleGameConversion constructor.
30
     *
31
     * @param $titles
32
     */
33 150
    public function __construct($titles)
34
    {
35 150
        $this->titles = new AssociativeArray($titles);
36
        // TODO call expansion api to complete the mapping.
37 150
    }
38
39
    /**
40
     * Get a compatible data.
41
     *
42
     * @param $haystack
43
     * @param $title
44
     * @param $mode
45
     * @param $script
46
     *
47
     * @return mixed|null
48
     */
49
    public function getCompatibleData($haystack, $title, $mode, $script)
50
    {
51
        // List of choices order by importance.
52
        $choices = $this->getChoicesByPriority($title, $mode, $script);
53
54
        foreach ($choices as $choice) {
55
            $data = AssociativeArray::getFromKey($haystack, $choice);
56
57
            if (!is_null($data)) {
58
                return $data;
59
            }
60
        }
61
62
        return null;
63
    }
64
65
    /**
66
     * Get list of choices to test by priority.
67
     *
68
     * @param string $titleId
69
     * @param string $mode
70
     * @param string $script
71
     *
72
     * @return array
73
     */
74 4
    public function getChoicesByPriority($titleId, $mode, $script)
75
    {
76 4
        $game = $this->getTitleGame($titleId);
77
        return [
78 4
            [$titleId, $mode, $script],
79 4
            [$titleId, $mode, self::COMPATIBLE_ALL],
80 4
            [$titleId, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
81
82
            // If perfect title is not found then fallback on game.
83 4
            [$game, $mode, $script],
84 4
            [$game, $mode, self::COMPATIBLE_ALL],
85 4
            [$game, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
86
87
            // For modes that are common to all titles.
88 4
            [self::COMPATIBLE_ALL, $mode, $script],
89 4
            [self::COMPATIBLE_ALL, $mode, self::COMPATIBLE_ALL],
90
91
            // For data providers compatible with every title/gamemode/script.
92 4
            [self::COMPATIBLE_ALL, self::COMPATIBLE_ALL, self::COMPATIBLE_ALL],
93
        ];
94
    }
95
96
    /**
97
     * Get the game of the title.
98
     *
99
     * @param string $titleId
100
     *
101
     * @return string
102
     */
103 4
    public function getTitleGame($titleId)
104
    {
105 4
        $game = $this->titles->get($titleId, self::GAME_UNKNOWN);
106 4
        return $game;
107
    }
108
}