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

Mini   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 49
c 1
b 0
f 0
dl 0
loc 106
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A numberFormat() 0 3 1
A getBadge() 0 7 2
A getAvatar() 0 7 2
A __invoke() 0 48 3
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\PictureCreatorFactory;
14
use VideoGamesRecords\CoreBundle\Traits\GetOrdinalSuffixTrait;
15
use VideoGamesRecords\CoreBundle\Traits\NumberFormatTrait;
16
17
/**
18
 * @Route("/gamercard")
19
 * @Cache(expires="tomorrow", public=true)
20
 */
21
class Mini extends AbstractController
22
{
23
    use GetOrdinalSuffixTrait;
24
    use NumberFormatTrait;
25
26
    private FilesystemOperator $appStorage;
27
28
    public function __construct(FilesystemOperator $appStorage) {
29
        $this->appStorage = $appStorage;
30
    }
31
32
    /**
33
     * @Route("/mini/{id}", name="gamercard_mini_1", methods={"GET"})
34
     * @Route("/{id}/mini", name="gamercard_mini_2", methods={"GET"})
35
     * @Cache(smaxage="900")
36
     * @param Player $player
37
     * @throws Exception
38
     * @throws FilesystemException
39
     */
40
    public function __invoke(Player $player): void
41
    {
42
        chdir(__DIR__);
43
        $gamercard = PictureCreatorFactory::fromFile('../../../Resources/img/gamercard/mini.png');
44
45
        // Ranking Points
46
        $fontSize = 8;
47
        $gamercard
48
            ->addColor('lightBrown', 255, 218, 176)
49
            ->addFont('segoeUISemiBold', '../../../Resources/fonts/seguisb.ttf')
50
            ->write($this->numberFormat($player->getPointGame()) . ' Pts', $fontSize, 40, 20)
51
            ->write('/', $fontSize, 124, 20)
52
            ->addColor('darkYellow', 255, 191, 1)
53
            ->write($player->getRankPointGame() . ' ' . $this->getOrdinalSuffix($player->getRankPointGame()), $fontSize, 130, 20);
54
55
56
        // Ranking Medals
57
        $sprite = PictureCreatorFactory::fromFile('../../../Resources/img/sprite.png');
58
        $gamercard
59
            ->copyResized($sprite, 164, 8, 126, 160, 16, 16, 16, 16)
60
            ->copyResized($sprite, 211, 8, 108, 160, 16, 16, 16, 16)
61
            ->copyResized($sprite, 258, 8, 92, 160, 16, 16, 16, 16)
62
            ->copyResized($sprite, 305, 8, 74, 160, 16, 16, 16, 16);
63
64
        $gamercard->getColor('lightBrown');
65
        $gamercard
66
            ->write($player->getChartRank0(), $fontSize, 180, 20)
67
            ->write($player->getChartRank1(), $fontSize, 227, 20)
68
            ->write($player->getChartRank2(), $fontSize, 274, 20)
69
            ->write($player->getChartRank3(), $fontSize, 321, 20);
70
        $gamercard->write('/', $fontSize, 350, 20);
71
        $gamercard->getColor('darkYellow');
72
        $rank = $player->getRankMedal();
73
        if ($rank <= 99) {
74
            $rank .= $this->getOrdinalSuffix($rank);
75
        }
76
        $gamercard->write($rank, $fontSize, 356, 20);
77
78
        // Add avatar
79
        $avatar = PictureCreatorFactory::fromStream($this->getAvatar($player));
80
        $gamercard->copyResized($avatar, 4, 2, 0, 0, 26, 26);
81
82
        try {
83
            $gamercard->downloadPicture('png', 'VGR-GamerCard-Mini-' . $player->getSlug() . '.png');
84
        } catch (Exception $e) {
85
            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...
86
        }
87
        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...
88
    }
89
90
91
    /**
92
     * @param      $value
93
     * @return string
94
     */
95
    private function numberFormat($value): string
96
    {
97
        return number_format($value);
98
    }
99
100
    /**
101
     * @param Player $player
102
     * @return string
103
     * @throws FilesystemException
104
     */
105
    public function getAvatar(Player $player): string
106
    {
107
        $path = 'user' . DIRECTORY_SEPARATOR . $player->getAvatar();
108
        if (!$this->appStorage->fileExists($path)) {
109
            $path = 'user' . DIRECTORY_SEPARATOR . 'default.png';
110
        }
111
        return $this->appStorage->read($path);
112
    }
113
114
115
    /**
116
     * @param Badge $badge
117
     * @return string
118
     * @throws FilesystemException
119
     */
120
    public function getBadge(Badge $badge): string
121
    {
122
        $path = 'badge' . DIRECTORY_SEPARATOR . $badge->getType() . DIRECTORY_SEPARATOR . $badge->getPicture();
123
        if (!$this->appStorage->fileExists($path)) {
124
            $path = 'badge' . DIRECTORY_SEPARATOR . 'default.gif';
125
        }
126
        return $this->appStorage->read($path);
127
    }
128
}
129