Passed
Push — develop ( 0b3512...4ec848 )
by BENARD
04:21
created

Classic   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 66
c 0
b 0
f 0
dl 0
loc 123
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBadge() 0 7 2
A __construct() 0 6 1
A getAvatar() 0 7 2
B __invoke() 0 69 5
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\Controller\Player\Gamercard;
4
5
use Exception;
6
use League\Flysystem\FilesystemException;
7
use League\Flysystem\FilesystemOperator;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...dle\Configuration\Cache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\Routing\Annotation\Route;
11
use VideoGamesRecords\CoreBundle\Entity\Badge;
12
use VideoGamesRecords\CoreBundle\Entity\Player;
13
use VideoGamesRecords\CoreBundle\File\Picture;
14
use VideoGamesRecords\CoreBundle\File\PictureCreatorFactory;
15
use VideoGamesRecords\CoreBundle\Repository\PlayerGameRepository;
16
use VideoGamesRecords\CoreBundle\Traits\GetOrdinalSuffixTrait;
17
use VideoGamesRecords\CoreBundle\Traits\NumberFormatTrait;
18
19
/**
20
 * @Route("/gamercard")
21
 * @Cache(expires="tomorrow", public=true)
22
 */
23
class Classic extends AbstractController
24
{
25
    use GetOrdinalSuffixTrait;
26
    use NumberFormatTrait;
27
28
    private FilesystemOperator $appStorage;
29
    private PlayerGameRepository $playerGameRepository;
30
31
    public function __construct(
32
        FilesystemOperator $appStorage,
33
        PlayerGameRepository $playerGameRepository
34
    ) {
35
        $this->appStorage = $appStorage;
36
        $this->playerGameRepository = $playerGameRepository;
37
    }
38
39
    /**
40
     * @Route("/{id}/classic", name="gamercard_classic_1", methods={"GET"})
41
     * @Route("/classic/{id}", name="gamercard_classic_2", methods={"GET"})
42
     * @Cache(smaxage="900")
43
     * @param Player $player
44
     * @throws FilesystemException
45
     * @throws Exception
46
     */
47
    public function __invoke(Player $player): void
48
    {
49
        chdir(__DIR__);
50
51
        $gamercard = new Picture(210, 135);
52
        $gamercard->addColor('black', 13, 14, 15)
53
            ->addRectangle(0, 0, 210, 24)
54
            ->addColor('grey', 58, 56, 56)
55
            ->addRectangle(0, 25, 210, 135)
56
            ->addColor('lightGrayLine', 196, 196, 196)
57
            ->addRectangle(78, 52, 204, 53)
58
            ->addColor('darkGrayLine', 86, 86, 86)
59
            ->addRectangle(78, 52, 203, 52);
60
61
        // Pseudo
62
        if ($player->getTeam() !== null) {
63
            $pseudo = sprintf('[%s] %s', $player->getTeam()->getTag(), $player->getPseudo());
64
        } else {
65
            $pseudo = $player->getPseudo();
66
        }
67
        $gamercard->addColor('orange', 246, 162, 83)
68
            ->addFont('segoeUILight', '../../../Resources/fonts/segoeuil.ttf')
69
            ->write($pseudo, 12.375, 9, 17);
70
71
        // Ranking
72
        $fontSize = 7.5;
73
        $rankMedal = '/' . $player->getRankMedal();
74
        if ($player->getRankMedal() <= 999) {
75
            $rankMedal .= $this->getOrdinalSuffix($player->getRankMedal());
76
        }
77
        $pointGame = $this->numberFormat($player->getPointGame()) . ' Pts / ';
78
        $pointGame .= $player->getRankPointGame() . $this->getOrdinalSuffix($player->getRankPointGame());
79
        $gamercard
80
            ->addColor('white', 255, 255, 255)
81
            ->addFont('segoeUISemiBold', '../../../Resources/fonts/seguisb.ttf')
82
            ->write($player->getChartRank0(), $fontSize, 96, 70)
83
            ->write($player->getChartRank1(), $fontSize, 145, 70)
84
            ->write($player->getChartRank2(), $fontSize, 96, 90)
85
            ->write($player->getChartRank3(), $fontSize, 145, 90)
86
            ->write($rankMedal, $fontSize, 175, 80)
87
            ->write($pointGame, $fontSize, 82, 45);
88
89
        // Add sprites pictures medals
90
        $sprite = PictureCreatorFactory::fromFile('../../../Resources/img/sprite.png');
91
        $gamercard
92
            ->copyResized($sprite, 78, 59, 126, 160, 16, 16, 16, 16)
93
            ->copyResized($sprite, 127, 59, 108, 160, 16, 16, 16, 16)
94
            ->copyResized($sprite, 78, 79, 92, 160, 16, 16, 16, 16)
95
            ->copyResized($sprite, 127, 79, 74, 160, 16, 16, 16, 16);
96
97
        // Add avatar
98
        $avatar = PictureCreatorFactory::fromStream($this->getAvatar($player));
99
        $gamercard->copyResized($avatar, 9, 30, 0, 0, 64, 64);
100
101
        $playerGames = $this->playerGameRepository->findBy(['player' => $player], ['lastUpdate' => 'DESC'], 5);
102
103
        $startX = 9;
104
        foreach ($playerGames as $playerGame) {
105
            $picture = PictureCreatorFactory::fromStream($this->getBadge($playerGame->getGame()->getBadge()));
106
            $gamercard->copyResized($picture, $startX, 99);
107
            $startX += 38;
108
        }
109
110
        try {
111
            $gamercard->downloadPicture('png', 'VGR-GamerCard-Classic-' . $player->getSlug() . '.png');
112
        } catch (Exception $e) {
113
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
114
        }
115
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
116
    }
117
118
119
    /**
120
     * @param Player $player
121
     * @return string
122
     * @throws FilesystemException
123
     */
124
    public function getAvatar(Player $player): string
125
    {
126
        $path = 'user' . DIRECTORY_SEPARATOR . $player->getAvatar();
127
        if (!$this->appStorage->fileExists($path)) {
128
            $path = 'user' . DIRECTORY_SEPARATOR . 'default.png';
129
        }
130
        return $this->appStorage->read($path);
131
    }
132
133
134
    /**
135
     * @param Badge $badge
136
     * @return string
137
     * @throws FilesystemException
138
     */
139
    public function getBadge(Badge $badge): string
140
    {
141
        $path = 'badge' . DIRECTORY_SEPARATOR . $badge->getType() . DIRECTORY_SEPARATOR . $badge->getPicture();
142
        if (!$this->appStorage->fileExists($path)) {
143
            $path = 'badge' . DIRECTORY_SEPARATOR . 'default.gif';
144
        }
145
        return $this->appStorage->read($path);
146
    }
147
}
148