Conditions | 7 |
Paths | 12 |
Total Lines | 51 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function __invoke(UpdateStream $message): void |
||
31 | { |
||
32 | $channels = $this->em->getRepository(Channel::class)->findAll(); |
||
33 | |||
34 | $usernames = []; |
||
35 | foreach ($channels as $channel) { |
||
36 | $usernames[] = $channel->getUsername(); |
||
37 | } |
||
38 | |||
39 | $response = $this->twitchItemDataProvider->getStreams($usernames); |
||
40 | $data = json_decode($response->getBody()->getContents())->data; |
||
41 | if (count($data) === 0) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | foreach ($data as $twicthStream) { |
||
46 | $findStream = $this->em->getRepository(Stream::class)->findOneBy(['externalId' => $twicthStream->id]); |
||
47 | |||
48 | |||
49 | if ($findStream) { |
||
50 | if ($twicthStream->viewer_count > $findStream->getViewerCount()) { |
||
51 | $findStream->setViewerCount((int) $twicthStream->viewer_count); |
||
52 | $this->em->flush(); |
||
53 | } |
||
54 | continue; |
||
55 | } |
||
56 | |||
57 | // Game |
||
58 | $game = $this->em->getRepository(Game::class)->findOneBy(['externalId' => $twicthStream->game_id]); |
||
59 | if (null == $game) { |
||
60 | $game = new Game(); |
||
61 | $game->setExternalId($twicthStream->game_id); |
||
62 | $game->setName($twicthStream->game_name); |
||
63 | $this->em->persist($game); |
||
64 | } |
||
65 | $game->setLastStreamAt(new DateTime()); |
||
66 | $this->em->flush(); |
||
67 | |||
68 | $entityStream = new Stream(); |
||
69 | $entityStream->setGame($game); |
||
70 | $entityStream->setChannel($this->em->getRepository(Channel::class)->findOneBy(['username' => $twicthStream->user_login])); |
||
|
|||
71 | $entityStream->setExternalId($twicthStream->id); |
||
72 | $entityStream->setTitle($twicthStream->title); |
||
73 | $entityStream->setType($twicthStream->type); |
||
74 | $entityStream->setStartedAt(new DateTime($twicthStream->started_at)); |
||
75 | $entityStream->setViewerCount((int) $twicthStream->viewer_count); |
||
76 | $entityStream->setLanguage($twicthStream->language); |
||
77 | $entityStream->setTags($twicthStream->tags); |
||
78 | $entityStream->setIsMature($twicthStream->is_mature); |
||
79 | $this->em->persist($entityStream); |
||
80 | $this->em->flush(); |
||
81 | } |
||
84 |