Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RadiumOflixPosterLoadCommand | |
0.00% |
0 / 45 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
configure | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace App\Command; |
4 | |
5 | use App\Repository\MovieRepository; |
6 | use Doctrine\ORM\EntityManagerInterface; |
7 | use Symfony\Component\Console\Command\Command; |
8 | use Symfony\Component\Console\Input\InputArgument; |
9 | use Symfony\Component\Console\Input\InputInterface; |
10 | use Symfony\Component\Console\Input\InputOption; |
11 | use Symfony\Component\Console\Output\OutputInterface; |
12 | use Symfony\Component\Console\Style\SymfonyStyle; |
13 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
14 | |
15 | class RadiumOflixPosterLoadCommand extends Command |
16 | { |
17 | protected static $defaultName = 'radium:oflix:poster-load'; |
18 | protected static $defaultDescription = 'met à jour tout les poster de la BDD avec OMDBAPI'; |
19 | |
20 | /** |
21 | * @var MovieRepository |
22 | */ |
23 | private $movieRepository; |
24 | |
25 | /** |
26 | * nous permet de faire des requetes HTTP |
27 | * |
28 | * @var HttpClientInterface |
29 | */ |
30 | private $client; |
31 | |
32 | /** |
33 | * uniquement pour le flush |
34 | * |
35 | * @var EntityManagerInterface |
36 | */ |
37 | private $entityManager; |
38 | |
39 | private $apiKey = "a93b767b"; |
40 | |
41 | public function __construct( |
42 | MovieRepository $movieRepository, |
43 | HttpClientInterface $httpClientInterface, |
44 | EntityManagerInterface $entityManager |
45 | ) |
46 | { |
47 | // ? https://symfony.com/doc/5.4/http_client.html |
48 | $this->client = $httpClientInterface; |
49 | |
50 | $this->movieRepository = $movieRepository; |
51 | |
52 | // uniquement pour le flush |
53 | $this->entityManager = $entityManager; |
54 | |
55 | // ! Command class "App\Command\RadiumOflixPosterLoadCommand" is not correctly initialized. You probably forgot to call the parent constructor. |
56 | // (un coup de fil à mamie) |
57 | parent::__construct(); |
58 | } |
59 | |
60 | protected function configure(): void |
61 | { |
62 | $this |
63 | ->addArgument( |
64 | // le nom du paramètre, va nous servir pour récupérer la valeur dans la fonction execute |
65 | 'movie_id', |
66 | // est ce que l'on est obligé de fournir l'argument ? |
67 | // OUI : InputArgument::REQUIRED |
68 | // NON : InputArgument::OPTIONAL |
69 | InputArgument::OPTIONAL, |
70 | // le message de decription pour l'aide |
71 | 'ID du film à mettre à jour') |
72 | |
73 | //->addOption('option1', null, InputOption::VALUE_NONE, 'Option description') |
74 | ; |
75 | } |
76 | |
77 | protected function execute(InputInterface $input, OutputInterface $output): int |
78 | { |
79 | // ? https://symfony.com/doc/5.4/console/style.html |
80 | // permet d'avoir accès a plein de méthode utilitaire dans le terminal |
81 | $io = new SymfonyStyle($input, $output); |
82 | |
83 | $movieID = $input->getArgument('movie_id'); |
84 | // puisque il est optionnel, je vérifie sa présence |
85 | // et je fait un code un peu différent |
86 | if ($movieID) { |
87 | // j'ai l'argument fournit |
88 | // TODO : aller chercher le film |
89 | $movie = $this->movieRepository->find($movieID); |
90 | // 2. on demande à L'API |
91 | $response = $this->client->request( |
92 | // 1. la méthode |
93 | 'GET', |
94 | // 2. URL |
95 | // on va créer l'URL avec le titre du film |
96 | "https://www.omdbapi.com/?apikey=" . $this->apiKey . "&t=". $movie->getTitle() |
97 | ); |
98 | // je récupère le contenu de la réponse en tableau associatif |
99 | $content = $response->toArray(); |
100 | |
101 | if (array_key_exists('Poster', $content)){ |
102 | $newPoster = $content['Poster']; |
103 | } else { |
104 | // il n'y pas de lien pour le poster |
105 | // on met une URL par défaut |
106 | $newPoster = "https://amc-theatres-res.cloudinary.com/amc-cdn/static/images/fallbacks/DefaultOneSheetPoster.jpg"; |
107 | } |
108 | |
109 | // 3t. on met à jour notre poster de notre film |
110 | $movie->setPoster($newPoster); |
111 | // 4. on flush |
112 | $this->entityManager->flush(); |
113 | |
114 | $io->success('le fim '.$movie->getTitle().' a bien été mis à jour'); |
115 | // je m'arrête ici avec un success |
116 | return Command::SUCCESS; |
117 | } |
118 | |
119 | |
120 | // TODO : aller chercher dans OMDBAPI les posters de chaque film |
121 | // 1. il nous faut la liste des films : BDD, Repository |
122 | // injection de dépendance, MAIS ... |
123 | // donc on utilise le __construct() |
124 | $allMovies = $this->movieRepository->findAll(); |
125 | |
126 | // 2. on boucle sur la liste des films |
127 | foreach ($allMovies as $movie) { |
128 | |
129 | // UX |
130 | $io->title($movie->getTitle()); |
131 | |
132 | // 3. pour chaque film, on demande à L'API |
133 | // pour faire des requetes HTTP, on utilise le service HttpClientInterface via l'injection de dépendance |
134 | $response = $this->client->request( |
135 | // 1. la méthode |
136 | 'GET', |
137 | // 2. URL |
138 | // on va créer l'URL avec le titre du film |
139 | "https://www.omdbapi.com/?apikey=" . $this->apiKey . "&t=". $movie->getTitle() |
140 | ); |
141 | |
142 | //dd($response); |
143 | $statusCode = $response->getStatusCode(); |
144 | // dd($statusCode);// $statusCode = 200 |
145 | $contentType = $response->getHeaders()['content-type'][0]; |
146 | // dd($contentType);// $contentType = 'application/json; charset=utf-8' |
147 | $content = $response->getContent(); |
148 | //dd($content); |
149 | // $content = '{"id":521583, "name":"symfony-docs", ...}' |
150 | $content = $response->toArray(); |
151 | // dd($content); |
152 | // $content = ['id' => 521583, 'name' => 'symfony-docs', ...] |
153 | |
154 | // 3b. on récupère l'URL du poster |
155 | // commen on a transformé le contenu de la réponse en tableau associatif |
156 | // on peut regarder la clé 'Poster' |
157 | // dd($content['Poster']); |
158 | if (array_key_exists('Poster', $content)){ |
159 | $newPoster = $content['Poster']; |
160 | } else { |
161 | // il n'y pas de lien pour le poster |
162 | // on met une URL par défaut |
163 | $newPoster = "https://amc-theatres-res.cloudinary.com/amc-cdn/static/images/fallbacks/DefaultOneSheetPoster.jpg"; |
164 | } |
165 | |
166 | // 3t. on met à jour notre poster de notre film |
167 | $movie->setPoster($newPoster); |
168 | |
169 | } |
170 | // 4. on flush |
171 | $this->entityManager->flush(); |
172 | |
173 | $io->success('Yahoo les fims sont à jour !'); |
174 | |
175 | return Command::SUCCESS; |
176 | |
177 | |
178 | /* exemple Gestion des arguments / options |
179 | $arg1 = $input->getArgument('arg1'); |
180 | |
181 | if ($arg1) { |
182 | $io->note(sprintf('You passed an argument: %s', $arg1)); |
183 | } |
184 | |
185 | if ($input->getOption('option1')) { |
186 | // ... |
187 | } |
188 | */ |
189 | } |
190 | } |