ImageProperties::detect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 17
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AhmadMayahi\Vision\Detectors;
6
7
use AhmadMayahi\Vision\Contracts\Detectable;
8
use AhmadMayahi\Vision\Data\ImageProperties as ImagePropertiesData;
9
use AhmadMayahi\Vision\Support\AbstractDetector;
10
use AhmadMayahi\Vision\Traits\Arrayable;
11
use AhmadMayahi\Vision\Traits\Jsonable;
12
use Generator;
13
use Google\Cloud\Vision\V1\ColorInfo;
14
use Google\Cloud\Vision\V1\ImageProperties as GoogleVisionImageProperties;
15
use Google\Type\Color;
16
17
class ImageProperties extends AbstractDetector implements Detectable
18
{
19
    use Arrayable;
20
    use Jsonable;
21
22
    public function getOriginalResponse(): ?GoogleVisionImageProperties
23
    {
24
        $response = $this
25
            ->imageAnnotatorClient
26
            ->imagePropertiesDetection($this->file->toVisionFile());
27
28
        return $response->getImagePropertiesAnnotation();
29
    }
30
31
    public function detect(): ?Generator
32
    {
33
        $response = $this->getOriginalResponse();
34
35
        if (! $response) {
36
            return null;
37
        }
38
39
        /** @var ColorInfo $colorInfo */
40
        foreach ($response->getDominantColors()->getColors() as $colorInfo) {
41
            /** @var Color $color */
42
            $color = $colorInfo->getColor();
43
            yield new ImagePropertiesData(
44
                pixelFraction: $colorInfo->getPixelFraction(),
45
                red: $color->getRed(),
46
                green: $color->getGreen(),
47
                blue: $color->getBlue(),
48
            );
49
        }
50
    }
51
}
52