Web::detect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 15
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\Web as WebData;
9
use AhmadMayahi\Vision\Data\WebEntity as WebEntityData;
10
use AhmadMayahi\Vision\Data\WebImage as WebImageData;
11
use AhmadMayahi\Vision\Data\WebPage as WebPageData;
12
use AhmadMayahi\Vision\Support\AbstractDetector;
13
use Google\Cloud\Vision\V1\WebDetection;
14
use Google\Cloud\Vision\V1\WebDetection\WebEntity;
15
use Google\Cloud\Vision\V1\WebDetection\WebImage;
16
use Google\Cloud\Vision\V1\WebDetection\WebLabel;
17
use Google\Cloud\Vision\V1\WebDetection\WebPage;
18
19
class Web extends AbstractDetector implements Detectable
20
{
21
    public function getOriginalResponse(): ?WebDetection
22
    {
23
        $web = $this
24
            ->imageAnnotatorClient
25
            ->webDetection($this->file->toVisionFile());
26
27
        return $web->getWebDetection();
28
    }
29
30
    public function detect(): ?WebData
31
    {
32
        $response = $this->getOriginalResponse();
33
34
        if (! $response) {
35
            return null;
36
        }
37
38
        return new WebData(
39
            $this->getBestGuessLabels($response),
40
            $this->getPagesWithMatchingImages($response),
41
            $this->getFullMatchingImages($response),
42
            $this->getPartialMatchingImages($response),
43
            $this->getVisuallySimilarImages($response),
44
            $this->getWebEntries($response)
45
        );
46
    }
47
48
    /**
49
     * @param WebDetection $response
50
     * @return array|string[]
51
     */
52
    protected function getBestGuessLabels(WebDetection $response): array
53
    {
54
        return array_map(function (WebLabel $label) {
55
            return $label->getLabel();
56
        }, iterator_to_array($response->getBestGuessLabels()));
57
    }
58
59
    /**
60
     * @param WebDetection $response
61
     * @return WebPageData[]
62
     */
63
    protected function getPagesWithMatchingImages(WebDetection $response): array
64
    {
65
        return array_map(function (WebPage $item) {
66
            return new WebPageData($item->getUrl(), $item->getPageTitle(), $item->getScore());
67
        }, iterator_to_array($response->getPagesWithMatchingImages()));
68
    }
69
70
    /**
71
     * @param WebDetection $response
72
     * @return WebImageData[]
73
     */
74
    protected function getFullMatchingImages(WebDetection $response): array
75
    {
76
        return array_map(function (WebImage $item) {
77
            return new WebImageData($item->getUrl(), $item->getScore());
78
        }, iterator_to_array($response->getFullMatchingImages()));
79
    }
80
81
    /**
82
     * @param WebDetection $response
83
     * @return WebImageData[]
84
     */
85
    protected function getPartialMatchingImages(WebDetection $response): array
86
    {
87
        return array_map(function (WebImage $item) {
88
            return new WebImageData($item->getUrl(), $item->getScore());
89
        }, iterator_to_array($response->getPartialMatchingImages()));
90
    }
91
92
    /**
93
     * @param WebDetection $response
94
     * @return WebImageData[]
95
     */
96
    protected function getVisuallySimilarImages(WebDetection $response): array
97
    {
98
        return array_map(function (WebImage $item) {
99
            return new WebImageData($item->getUrl(), $item->getScore());
100
        }, iterator_to_array($response->getVisuallySimilarImages()));
101
    }
102
103
    /**
104
     * @param WebDetection $response
105
     * @return WebEntityData[]
106
     */
107
    protected function getWebEntries(WebDetection $response): array
108
    {
109
        return array_map(function (WebEntity $item) {
110
            return new WebEntityData($item->getEntityId(), $item->getScore(), $item->getDescription());
111
        }, iterator_to_array($response->getWebEntities()));
112
    }
113
}
114