| Conditions | 6 |
| Paths | 6 |
| Total Lines | 77 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 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 |
||
| 73 | public function handle(Repository $config, Container $container) |
||
| 74 | { |
||
| 75 | $this->syncUsers($config, $container); |
||
| 76 | |||
| 77 | $config->set('gitter.output', false); |
||
| 78 | |||
| 79 | $client = Client::make($config->get('gitter.token'), $this->argument('room')); |
||
| 80 | $room = $container->make(Room::class); |
||
| 81 | |||
| 82 | $this->karma = new Validator(); |
||
| 83 | |||
| 84 | |||
| 85 | $request = $this->cursor($client, $room); |
||
| 86 | $count = 1; // Start number |
||
| 87 | $page = 0; // Current page |
||
| 88 | $chunk = 1000; // Per page |
||
| 89 | |||
| 90 | |||
| 91 | while (true) { |
||
| 92 | $messageChunk = $request($chunk, $chunk * $page++); |
||
| 93 | |||
| 94 | if (!count($messageChunk)) { |
||
| 95 | $this->output->write(sprintf("\r Well done. <comment>%s</comment> Messages was be loaded.", $count)); |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | foreach ($messageChunk as $m) { |
||
| 101 | echo "\rLoad message: $count "; |
||
| 102 | $count++; |
||
| 103 | } |
||
| 104 | |||
| 105 | $name = 'sync/' . $page . '.json'; |
||
| 106 | echo '...dump to ' . $name; |
||
| 107 | file_put_contents( |
||
| 108 | storage_path($name), |
||
| 109 | json_encode($messageChunk) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | echo "\n"; |
||
| 115 | |||
| 116 | $this->output->write('Flush database karma increments'); |
||
| 117 | Karma::query() |
||
| 118 | ->where('room_id', $room->id) |
||
| 119 | ->delete(); |
||
| 120 | |||
| 121 | |||
| 122 | $this->output->write('Start message parsing.'); |
||
| 123 | $finder = (new Finder()) |
||
| 124 | ->files() |
||
| 125 | ->in(storage_path('sync')) |
||
| 126 | ->name('*.json') |
||
| 127 | ->sort(function($a, $b) { |
||
| 128 | $parse = function(\SplFileInfo $file) { |
||
| 129 | return str_replace('.json', '', $file->getFilename()); |
||
| 130 | }; |
||
| 131 | |||
| 132 | return $parse($b) <=> $parse($a); |
||
| 133 | }); |
||
| 134 | |||
| 135 | |||
| 136 | $count = 1; |
||
| 137 | foreach ($finder as $file) { |
||
| 138 | $messages = json_decode($file->getContents(), true); |
||
| 139 | foreach ($messages as $message) { |
||
| 140 | $message = Message::fromGitterObject($message); |
||
| 141 | |||
| 142 | echo "\r" . $count++ . ' messages parsing: ' . $message->created_at; |
||
| 143 | usleep(100); |
||
| 144 | $this->onMessage($message); |
||
| 145 | } |
||
| 146 | |||
| 147 | unlink($file->getRealPath()); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 217 |