Passed
Push — main ( f3cfb1...17781b )
by Emil
04:34
created

JasonController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Test Coverage

Coverage 99.27%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 111
dl 0
loc 239
rs 10
c 1
b 0
f 0
ccs 136
cts 137
cp 0.9927
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A apiDeckShuffle() 0 21 1
A apiLibraryBookISBN() 0 16 1
A api() 0 23 3
A apiGame() 0 16 1
B apiDeckDrawMany() 0 43 6
A quote() 0 45 1
A apiDeckDraw() 0 26 2
A apiLibraryBooks() 0 15 1
A apiDeck() 0 16 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Cards\CardHand;
6
use App\Cards\DeckOfCards;
7
use App\Game\BlackJack;
8
use App\Repository\BookRepository;
9
use Doctrine\Persistence\ManagerRegistry;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\Session\SessionInterface;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class JasonController extends AbstractController
17
{
18 1
    #[Route('/api', name: 'api')]
19
    public function api(
20
        ManagerRegistry $doctrine,
21
    ): Response {
22 1
        $bookRepository = new BookRepository($doctrine);
23
24 1
        $data = $bookRepository->readAllBooks();
25
26
        // If there is books in the library
27 1
        $isbn = null;
28 1
        if (count($data['books']) > 0) {
29 1
            $isbn = $data['books'][0]['isbn'];
30
        }
31
32 1
        if (null === $isbn) {
33
            $isbn = '0';
34
        }
35
36 1
        $data = [
37 1
            'isbn' => $isbn,
38 1
        ];
39
40 1
        return $this->render('api.html.twig', $data);
41
    }
42
43 1
    #[Route('/api/quote', name: 'api/quote', methods: ['GET'])]
44
    public function quote(): Response
45
    {
46 1
        $quotes = [
47 1
            [
48 1
                'quote' => "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to make it fast.",
49 1
                'author' => 'Joshua Bloch, in an interview by Peter Seibel in Coders At Work book',
50 1
            ],
51 1
            [
52 1
                'quote' => 'Playing with pointers is like playing with fire. Fire is perhaps the most important tool known to man. Carefully used, fire brings enormous benefits; but when fire gets out of control, disaster strikes.',
53 1
                'author' => 'John Barnes, Programming in Ada 2012, Cambridge University Press, 2014, p. 189',
54 1
            ],
55 1
            [
56 1
                'quote' => 'Applications programming is a race between software engineers, who strive to produce idiot-proof programs, and the universe which strives to produce bigger idiots. So far the Universe is winning.',
57 1
                'author' => 'Rick Cook, The Wizardry Compiled (1989) Ch. 6',
58 1
            ],
59 1
            [
60 1
                'quote' => "Computers are man's attempt at designing a cat: it does whatever it wants, whenever it wants, and rarely ever at the right time.",
61 1
                'author' => 'EMCIC, Keenspot Elf Life Forum, 2001-Apr-26',
62 1
            ],
63 1
            [
64 1
                'quote' => 'There is no programming language, no matter how structured, that will prevent programmers from making bad programs.',
65 1
                'author' => "Larry Flon (1975) 'On research in structured programming'. SIGPLAN Not., 10(10), pp.16–17",
66 1
            ],
67 1
            [
68 1
                'quote' => 'The main activity of programming is not the origination of new independent programs, but in the integration, modification, and explanation of existing ones.',
69 1
                'author' => "Terry Winograd (1991) 'Beyond Programming Languages', in Artificial intelligence & software engineering, ed. Derek Partridge, p. 317",
70 1
            ],
71 1
        ];
72
73 1
        $randInt = random_int(0, count($quotes) - 1);
74
75 1
        $data = [
76 1
            'quote' => $quotes[$randInt]['quote'],
77 1
            'author' => $quotes[$randInt]['author'],
78 1
            'timestamp' => date('d/m-y H:i:s', time()),
79 1
        ];
80
81 1
        $response = new JsonResponse($data);
82
83 1
        $response->setEncodingOptions(
84 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
85 1
        );
86
87 1
        return $response;
88
    }
89
90 1
    #[Route('/api/deck', name: 'api/deck', methods: ['GET'])]
91
    public function apiDeck(): Response
92
    {
93 1
        $deck = new DeckOfCards();
94
95 1
        $data = [
96 1
            'deck' => $deck->getString(),
97 1
        ];
98
99 1
        $response = new JsonResponse($data);
100
101 1
        $response->setEncodingOptions(
102 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
103 1
        );
104
105 1
        return $response;
106
    }
107
108 1
    #[Route('/api/deck/shuffle', name: 'api/deck/shuffle', methods: ['POST'])]
109
    public function apiDeckShuffle(
110
        SessionInterface $session,
111
    ): Response {
112 1
        $deck = new DeckOfCards();
113
114 1
        $deck->shuffleDeck();
115
116 1
        $session->set('cards_deck', $deck);
117
118 1
        $data = [
119 1
            'deck' => $deck->getString(),
120 1
        ];
121
122 1
        $response = new JsonResponse($data);
123
124 1
        $response->setEncodingOptions(
125 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
126 1
        );
127
128 1
        return $response;
129
    }
130
131 2
    #[Route('/api/deck/draw', name: 'api/deck/draw', methods: ['POST'])]
132
    public function apiDeckDraw(
133
        SessionInterface $session,
134
    ): Response {
135
        /** @var DeckOfCards $deck */
136 2
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
137 2
        $hand = new CardHand();
138
139 2
        ($deck->cardCount() > 0) ?
140 1
        $hand->addCard($deck->drawCard()) :
141 1
        throw new \RuntimeException("Can't draw more cards as the deck is empty!");
142
143 1
        $session->set('cards_deck', $deck);
144
145 1
        $data = [
146 1
            'hand' => $hand->getString(),
147 1
            'deckCount' => $deck->cardCount(),
148 1
        ];
149
150 1
        $response = new JsonResponse($data);
151
152 1
        $response->setEncodingOptions(
153 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
154 1
        );
155
156 1
        return $response;
157
    }
158
159 6
    #[Route("/api/deck/draw/{num<\d+>}", name: 'api/deck/draw/:number', methods: ['POST'])]
160
    public function apiDeckDrawMany(
161
        int $num,
162
        SessionInterface $session,
163
    ): Response {
164 6
        if ($num > 52) {
165 1
            throw new \RuntimeException("Can't draw more than cards in deck!");
166
        }
167 5
        if ($num < 1) {
168 1
            throw new \RuntimeException("Can't draw less than 1 card!");
169
        }
170
171
        /** @var DeckOfCards $deck */
172 4
        $deck = $session->get('cards_deck') ?? new DeckOfCards();
173 4
        $hand = new CardHand();
174
175 4
        $cardCount = $deck->cardCount();
176 4
        if (0 == $cardCount) {
177 1
            throw new \RuntimeException("Can't draw more cards as the deck is empty!");
178
        }
179 4
        if ($cardCount < $num) {
180 1
            throw new \RuntimeException("Can't draw more cards as the deck currently have!\n
181 1
            The deck currently have ".$cardCount.' many cards in the deck.');
182
        }
183
184 4
        for ($i = 0; $i < $num; ++$i) {
185 4
            $hand->addCard($deck->drawCard());
186
        }
187
188 4
        $session->set('cards_deck', $deck);
189
190 4
        $data = [
191 4
            'hand' => $hand->getString(),
192 4
            'deckCount' => $deck->cardCount(),
193 4
        ];
194
195 4
        $response = new JsonResponse($data);
196
197 4
        $response->setEncodingOptions(
198 4
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
199 4
        );
200
201 4
        return $response;
202
    }
203
204 1
    #[Route('/api/game', name: 'api/game', methods: ['GET'])]
205
    public function apiGame(
206
        SessionInterface $session,
207
    ): Response {
208
        /** @var BlackJack $blackJack */
209 1
        $blackJack = $session->get('black_jack') ?? new BlackJack();
210
211 1
        $data = $blackJack->stateOfGame();
212
213 1
        $response = new JsonResponse($data);
214
215 1
        $response->setEncodingOptions(
216 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
217 1
        );
218
219 1
        return $response;
220
    }
221
222 1
    #[Route('api/library/books', name: 'api/library/books', methods: ['GET'])]
223
    public function apiLibraryBooks(
224
        ManagerRegistry $doctrine,
225
    ): Response {
226 1
        $bookRepository = new BookRepository($doctrine);
227
228 1
        $data = $bookRepository->readAllBooks();
229
230 1
        $response = new JsonResponse($data);
231
232 1
        $response->setEncodingOptions(
233 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
234 1
        );
235
236 1
        return $response;
237
    }
238
239 1
    #[Route("api/library/book/{isbn<\d+>}", name: 'api/library/book/isbn', methods: ['GET'])]
240
    public function apiLibraryBookISBN(
241
        string $isbn,
242
        ManagerRegistry $doctrine,
243
    ): Response {
244 1
        $bookRepository = new BookRepository($doctrine);
245
246 1
        $data = $bookRepository->readOneBookISBN($isbn);
247
248 1
        $response = new JsonResponse($data);
249
250 1
        $response->setEncodingOptions(
251 1
            $response->getEncodingOptions() | JSON_PRETTY_PRINT
252 1
        );
253
254 1
        return $response;
255
    }
256
}
257