Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| FindsContentLengthForImageObject | |
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isImageStoredRemotely | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fetchRemoteImageInformation | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| fetchLocalImageInformation | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| write | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Actions; |
| 4 | |
| 5 | use Hyde\Framework\Contracts\ActionContract; |
| 6 | use Hyde\Framework\Models\Image; |
| 7 | use Illuminate\Support\Facades\Http; |
| 8 | use Symfony\Component\Console\Output\ConsoleOutput; |
| 9 | use Symfony\Component\Console\Output\OutputInterface; |
| 10 | |
| 11 | /** |
| 12 | * @see \Tests\Feature\FindsContentLengthForImageObjectTest |
| 13 | */ |
| 14 | class FindsContentLengthForImageObject implements ActionContract |
| 15 | { |
| 16 | protected Image $image; |
| 17 | |
| 18 | /** |
| 19 | * Testing adding console debug output. |
| 20 | */ |
| 21 | protected OutputInterface $output; |
| 22 | |
| 23 | public function __construct(Image $image) |
| 24 | { |
| 25 | $this->image = $image; |
| 26 | |
| 27 | $this->output = new ConsoleOutput(); |
| 28 | } |
| 29 | |
| 30 | public function execute(): int |
| 31 | { |
| 32 | if ($this->isImageStoredRemotely()) { |
| 33 | return $this->fetchRemoteImageInformation(); |
| 34 | } |
| 35 | |
| 36 | return $this->fetchLocalImageInformation(); |
| 37 | } |
| 38 | |
| 39 | protected function isImageStoredRemotely(): bool |
| 40 | { |
| 41 | return str_starts_with($this->image->getSource(), 'http'); |
| 42 | } |
| 43 | |
| 44 | protected function fetchRemoteImageInformation(): int |
| 45 | { |
| 46 | $this->write('<fg=gray> ></> <fg=gray>Fetching remote image information for '.basename($this->image->getSource()).'...</>'); |
| 47 | |
| 48 | $response = Http::withHeaders([ |
| 49 | 'User-Agent' => config('hyde.http_user_agent', 'RSS Request Client'), |
| 50 | ])->head($this->image->getSource()); |
| 51 | |
| 52 | $headers = $response->headers(); |
| 53 | |
| 54 | if (array_key_exists('Content-Length', $headers)) { |
| 55 | return (int) key(array_flip($headers['Content-Length'])); |
| 56 | } |
| 57 | |
| 58 | $this->write(' > <comment>Warning:</comment> Could not find content length in headers for '.basename($this->image->getSource().'!')); |
| 59 | $this->write(' <fg=gray> Using default content length of 0. '.'</>'); |
| 60 | $this->write(' <fg=gray> Is the image path valid? '.($this->image->getSource()).'</>'); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | protected function fetchLocalImageInformation(): int |
| 66 | { |
| 67 | if (! file_exists($this->image->getSource())) { |
| 68 | $this->write(' > <comment>Warning:</comment> Could not find image file at '.$this->image->getSource().'!'); |
| 69 | $this->write(' <fg=gray> Using default content length of 0. '.'</>'); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | return filesize($this->image->getSource()); |
| 75 | } |
| 76 | |
| 77 | protected function write(string $string): void |
| 78 | { |
| 79 | $this->output->writeln($string); |
| 80 | } |
| 81 | } |