Issues (25)

src/Images/ImageUtils.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace Goose\Images;
4
5
use Goose\Configuration;
6
use GuzzleHttp\{Client, Pool};
7
use GuzzleHttp\Psr7\Request;
8
9
/**
10
 * Image Utils
11
 *
12
 * @package Goose\Images
13
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
14
 */
15
class ImageUtils {
16
    /**
17
     * @param string $filePath
18
     *
19
     * @return object|null
20
     */
21
    public static function getImageDimensions(string $filePath): ?\stdClass {
22
        list($width, $height, $type) = getimagesize($filePath);
23
24
        if ($type === null) {
25
            return null;
26
        }
27
28
        return (object)[
29
            'width' => (int)$width,
30
            'height' => (int)$height,
31
            'mime' => image_type_to_mime_type($type),
32
        ];
33
    }
34
35
    /**
36
     * Writes an image src http string to disk as a temporary file and returns the LocallyStoredImage object that has the info you should need
37
     * on the image
38
     *
39
     * @param string[] $imageSrcs
40
     * @param bool $returnAll
41
     * @param Configuration $config
42
     *
43
     * @return LocallyStoredImage[]
44
     */
45
    public static function storeImagesToLocalFile($imageSrcs, bool $returnAll, Configuration $config): array {
46
        $localImages = self::handleEntity($imageSrcs, $returnAll, $config);
0 ignored issues
show
Are you sure the assignment to $localImages is correct as self::handleEntity($imag...s, $returnAll, $config) targeting Goose\Images\ImageUtils::handleEntity() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
48
        if (empty($localImages)) {
49
            return [];
50
        }
51
52
        $locallyStoredImages = [];
53
54
        foreach ($localImages as $localImage) {
55
            if (empty($localImage->file) || !filesize($localImage->file)) {
56
                continue;
57
            }
58
59
            $imageDetails = self::getImageDimensions($localImage->file);
60
61
            if ($imageDetails !== null) {
62
                $locallyStoredImages[] = new LocallyStoredImage([
63
                    'imgSrc' => $localImage->url,
64
                    'localFileName' => $localImage->file,
65
                    'bytes' => filesize($localImage->file),
66
                    'height' => $imageDetails->height,
67
                    'width' => $imageDetails->width,
68
                    'fileExtension' => self::getFileExtensionName($imageDetails),
69
                ]);
70
            }
71
        }
72
73
        return $locallyStoredImages;
74
    }
75
76
    /**
77
     * @param object $imageDetails
78
     *
79
     * @return string
80
     */
81
    private static function getFileExtensionName(\stdClass $imageDetails): string {
82
        $extensions = [
83
            'image/gif' => '.gif',
84
            'image/jpeg' => '.jpg',
85
            'image/png' => '.png',
86
        ];
87
88
        return (
89
            isset($extensions[$imageDetails->mime])
90
            ? $extensions[$imageDetails->mime]
91
            : 'NA'
92
        );
93
    }
94
95
    /**
96
     * @param string[] $imageSrcs
97
     * @param bool $returnAll
98
     * @param Configuration $config
99
     *
100
     * @return array|null
101
     */
102
    private static function handleEntity($imageSrcs, bool $returnAll, Configuration $config): ?array {
103
        $guzzle = new Client();
104
105
        $results = [];
106
107
        $requests = function($urls) use ($guzzle, &$results) {
108
            foreach ($urls as $key => $url) {
109
                $file = tempnam(sys_get_temp_dir(), 'goose');
110
111
                $results[] = (object)[
112
                    'url' => $url,
113
                    'file' => $file,
114
                ];
115
116
                yield $key => function($options) use ($guzzle, $url, $file) {
117
                    $options['sink'] = $file;
118
119
                    return $guzzle->sendAsync(new Request('GET', $url), $options);
120
                };
121
            }
122
        };
123
124
        $pool = new Pool($guzzle, $requests($imageSrcs), [
125
            'concurrency' => 25,
126
            'fulfilled' => function($response, $index) use (&$results, $returnAll) {
127
                if (!$returnAll && $response->getStatusCode() != 200) {
128
                    unset($results[$index]);
129
                }
130
            },
131
            'rejected' => function($reason, $index) use (&$results, $returnAll) {
132
                if ($returnAll) {
133
                    $results[$index]->file = null;
134
                } else {
135
                    unset($results[$index]);
136
                }
137
            },
138
            'options' => $config->get('browser'),
139
        ]);
140
141
        $pool->promise()->wait();
142
143
        if (empty($results)) {
144
            return null;
145
        }
146
147
        return $results;
148
    }
149
}
150