Completed
Push — master ( 58e1a3...56d6f2 )
by Maik
01:20
created

QueryResultParser::parseRelatedTopics()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 9
Ratio 45 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 9
loc 20
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 2
crap 5
1
<?php
2
namespace Nkey\DDG\API;
3
4
use Nkey\DDG\API\Model\Icon;
5
use Nkey\DDG\API\Model\QueryResult;
6
use Nkey\DDG\API\Model\Result;
7
use Webmozart\Json\JsonDecoder;
8
use ReflectionClass;
9
use ReflectionMethod;
10
11
/**
12
 * This class provides means for parsing json from duckduckgo api into a local model
13
 *
14
 * @author Maik Greubel <[email protected]>
15
 * @license Apache 2.0
16
 */
17
class QueryResultParser
18
{
19
20
    /**
21
     * Parse a payload json string
22
     *
23
     * @param string $payload
24
     *            The payload to parse
25
     * @return QueryResult
26
     */
27 3
    public function parseQueryResult(string $payload): QueryResult
28
    {
29 3
        $decoder = new JsonDecoder();
30
        
31 3
        $json = $decoder->decode($payload);
32
        
33 3
        $result = new QueryResult();
34
        
35 3
        $ref = new ReflectionClass($result);
36 3
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
37 3 View Code Duplication
        foreach ($methods as $method) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38 3
            if (($propertyName = $this->getPropertyName($method))) {
39 3
                if ($propertyName === 'Meta') {
40 3
                    $result = $this->parseMeta($result, $json->meta);
41 3
                    continue;
42
                }
43 3
                $this->assignPropertyValue($method, $result, $json, $propertyName);
44
            }
45
        }
46
        
47 3
        $result = $this->parseResults($result, $json->RelatedTopics, false);
48 3
        $result = $this->parseResults($result, $json->Results, true);
49
        
50 3
        return $result;
51
    }
52
    
53 3
    private function assignPropertyValue(ReflectionMethod $method, $destObject, $object, $propertyName) {
54 3
        if (property_exists($object, $propertyName)) {
55 3
            $property = $object->$propertyName;
56 3
            $method->invoke($destObject, $property);
57
        }
58 3
    }
59
    
60 3
    private function getPropertyName(ReflectionMethod $method) {
61 3
        $propertyName = false;
62 3
        if (substr($method->name, 0, 3) === 'set') {
63 3
            $propertyName = substr($method->name, 3);
64
        }
65
        
66 3
        return $propertyName;
67
    }
68
69 3
    private function parseResults(QueryResult $queryResult, $results, bool $isResult)
70
    {
71 3
        foreach ($results as $result) {
72 3
            $res = new Result();
73 3
            $ref = new ReflectionClass($res);
74 3
            $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
75 3 View Code Duplication
            foreach ($methods as $method) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 3
                if (($propertyName = $this->getPropertyName($method))) {
77 3
                    if ($propertyName === 'Icon') {
78 3
                        $res = $this->parseIcon($res, $result->Icon);
79 3
                        continue;
80
                    }
81 3
                    $this->assignPropertyValue($method, $res, $result, $propertyName);
82
                }
83
            }
84 3
            if( $isResult ) {
85 1
                $queryResult->addResult($res);
86
            }
87
            else {
88 3
                $queryResult->addRelatedTopic($res);
89
            }
90
        }
91
        
92 3
        return $queryResult;
93
    }
94
95 3
    private function parseIcon(Result $result, $icon): Result
96
    {
97 3
        $ico = new Icon();
98
        
99 3
        $ref = new ReflectionClass($ico);
100 3
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
101 3
        foreach ($methods as $method) {
102 3
            if (($propertyName = $this->getPropertyName($method))) {
103 3
                if (property_exists($icon, $propertyName)) {
104 3
                    $property = $icon->$propertyName;
105
                    
106 3
                    if ($propertyName === 'Height' || $propertyName === 'Width') {
107 3
                        $property = intval($property);
108
                    }
109 3
                    $method->invoke($ico, $property);
110
                }
111
            }
112
        }
113
        
114 3
        $result->setIcon($ico);
115
        
116 3
        return $result;
117
    }
118
119 3
    private function parseMeta(QueryResult $result, $meta): QueryResult
0 ignored issues
show
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121 3
        return $result;
122
    }
123
}