Completed
Push — master ( 78d742...927789 )
by Jérémy
04:25
created

Pokedex   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 97
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 23 3
B getByFamily() 0 30 4
A morph() 0 22 2
1
<?php
2
3
/*
4
 * (c) Jérémy Marodon <[email protected]>
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Th3Mouk\PokemonGoIVCalculator\Extractors;
10
11
use Illuminate\Support\Collection;
12
use Th3Mouk\PokemonGoIVCalculator\Entities\Pokemon;
13
use Th3Mouk\PokemonGoIVCalculator\Exceptions\FamilyNotFound;
14
use Th3Mouk\PokemonGoIVCalculator\Exceptions\PokemonNotFound;
15
16
final class Pokedex extends GameMasterExtractor
17
{
18
    /**
19
     * Retrieve a Pokemon object from GameMaster file
20
     * @param  string          $name
21
     * @throws PokemonNotFound
22
     * @return Pokemon
23
     */
24
    public function get(string $name): Pokemon
25
    {
26
        $collection = $this->getGameMasterJsonCollection();
27
28
        $finded = $collection
29
            ->filter(function ($line) {
30
                if (isset($line->pokemonSettings)) {
31
                    return true;
32
                }
33
                return false;
34
            })
35
            ->filter(function ($line) use ($name) {
36
                return $line->pokemonSettings->pokemonId === strtoupper($name);
37
            })
38
            ->first()
39
        ;
40
41
        if (null === $finded) {
42
            throw new PokemonNotFound(strtolower($name));
43
        }
44
45
        return $this->morph($finded);
46
    }
47
48
    /**
49
     * Retrieve a collection of Pokemon object from GameMaster file
50
     * @param  string         $family family name prefixed
51
     * @throws FamilyNotFound
52
     * @return Collection
53
     */
54
    public function getByFamily(string $family): Collection
55
    {
56
        $collection = $this->getGameMasterJsonCollection();
57
58
        $finded = $collection
59
            ->filter(function ($line) {
60
                if (isset($line->pokemonSettings)) {
61
                    return true;
62
                }
63
                return false;
64
            })
65
            ->filter(function ($line) use ($family) {
66
                return $line->pokemonSettings->familyId === strtoupper($family);
67
            })
68
            ->sortBy(function ($line) {
69
                return $line->pokemonSettings->stats->baseStamina;
70
            })
71
        ;
72
73
        if (null === $finded) {
74
            throw new FamilyNotFound(strtolower($family));
75
        }
76
77
        $morphed = [];
78
        foreach ($finded as $found) {
79
            $morphed[] = $this->morph($found);
80
        }
81
82
        return collect($morphed);
83
    }
84
85
    /**
86
     * Transform raw datas from GameMaster file to Pokemon Object
87
     * @param $pokemon
88
     * @return Pokemon
89
     */
90
    private function morph($pokemon)
91
    {
92
        $types = [];
93
94
        $types[] = $pokemon->pokemonSettings->type;
95
        if (property_exists($pokemon->pokemonSettings, 'type2')) {
96
            $types[] = $pokemon->pokemonSettings->type2;
97
        }
98
99
        $res = null;
100
        preg_match('/[0-9]{4}/', $pokemon->templateId, $res);
101
102
        return new Pokemon(
103
            (int) current($res),
104
            strtolower($pokemon->pokemonSettings->pokemonId),
105
            $pokemon->pokemonSettings->familyId,
106
            $pokemon->pokemonSettings->stats->baseAttack,
107
            $pokemon->pokemonSettings->stats->baseDefense,
108
            $pokemon->pokemonSettings->stats->baseStamina,
109
            $types
110
        );
111
    }
112
}
113