Passed
Push — master ( f2f8c0...5c7f99 )
by Caen
02:56 queued 13s
created

FindsContentLengthForImageObject::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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