Completed
Push — master ( 182076...c80357 )
by
unknown
04:04
created

PageService::fetch()   F

Complexity

Conditions 12
Paths 532

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 12.8652

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 18
cts 22
cp 0.8182
rs 3.589
c 0
b 0
f 0
cc 12
nc 532
nop 4
crap 12.8652

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
/**
10
 * Description of PageService
11
 *
12
 * @author matias
13
 */
14
15
namespace Columnis\Service;
16
17
use Columnis\Model\Page;
18
use Columnis\Model\ApiResponse;
19
use Columnis\Model\PageLegacyData;
20
use Columnis\Exception\Api\ApiRequestException;
21
use Columnis\Exception\Templates\PathNotFoundException;
22
use Columnis\Exception\Templates\TemplateNameNotSetException;
23
use Columnis\Exception\Page\PageWithoutTemplateException;
24
use Columnis\Exception\Api\UnauthorizedException;
25
26
class PageService {
27
28
    /**
29
     * Api Service
30
     * @var ApiService $apiService
31
     */
32
    protected $apiService;
33
34
    /**
35
     * Template Service
36
     * @var TemplateService $templateService
37
     */
38
    protected $templateService;
39
40
    /**
41
     * PageBreakpoint Service
42
     * @var PageBreakpointService $pageBreakpointService
43
     */
44
    protected $pageBreakpointService;
45
46
    /**
47
     * Returns the Api Service
48 5
     * @return ApiService
49 5
     */
50
    public function getApiService() {
51
        return $this->apiService;
52
    }
53
54
    /**
55
     * Sets the Api Service
56 5
     * @param ApiService $apiService
57 5
     */
58 5
    public function setApiService(ApiService $apiService) {
59
        $this->apiService = $apiService;
60
    }
61
62
    /**
63
     * Returns the Template Service
64 4
     * @return TemplateService
65 4
     */
66
    public function getTemplateService() {
67
        return $this->templateService;
68
    }
69
70
    /**
71
     * Sets the Template Service
72 5
     * @param TemplateService $templateService
73 5
     */
74 5
    public function setTemplateService(TemplateService $templateService) {
75
        $this->templateService = $templateService;
76
    }
77
78
    /**
79
     * Returns the PageBreakpoint Service
80 1
     * @return PageBreakpointService
81 1
     */
82
    public function getPageBreakpointService() {
83
        return $this->pageBreakpointService;
84
    }
85
86
    /**
87
     * Sets the PageBreakpoint Service
88 4
     * @param PageBreakpointService $pageBreakpointService
89 4
     */
90 4
    public function setPageBreakpointService(PageBreakpointService $pageBreakpointService = null) {
91
        $this->pageBreakpointService = $pageBreakpointService;
92 4
    }
93 4
94 4
    public function __construct(TemplateService $templateService, ApiService $apiService, PageBreakpointService $pageBreakpointService = null) {
95 4
        $this->setTemplateService($templateService);
96 4
        $this->setApiService($apiService);
97
        $this->setPageBreakpointService($pageBreakpointService);
98
    }
99
100
    /**
101
     * Fetchs the page content from Columnis Api
102
     *
103
     * @param Page $page
104
     * @param Array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105 4
     * @return boolean
106 4
     */
107 4
    public function fetch(Page $page, Array $queryString = null, $accessToken = null, $retry = 0) {
108 4
        $id = $page->getId();
109
        $endpoint = '/pages/'.$id.'/generate';
110 4
        $uri = $this->getApiService()->getUri($endpoint);
111
        $lang = $this->getApiService()->parseLang($queryString['lang']);
112 3
        
113 3
        $headers = array(
114
            'Accept' => 'application/vnd.columnis.v2+json',
115 3
            'Content-Type' => 'application/json'
116 3
        );
117
        if (!empty($accessToken)) {
118 1
            $headers['Authorization'] = sprintf('Bearer %s', $accessToken);
119 1
        }
120
        if(!empty($lang)){
121
            $headers['Accept-Language'] = $lang;
122
        }
123
        
124
        $options = $this->getApiService()->buildOptions(array(), $queryString, $headers);
125 1
        try {
126 1
            $response = $this->getApiService()->request($uri, 'GET', $options);
127 4
            /* @var $response ApiResponse */
128 1
            $data = $response->getData();
129 2
130 1
            //Get page data
131 1
            $dataPagina = is_array($data['columnis.rest.pages']) ? array_values($data['columnis.rest.pages'])[0] : [];
132 1
            
133
            $templateService = $this->getTemplateService();
134 1
            $template = $templateService->createFromData($dataPagina);
135
136
            $pageBreakpointService = $this->getPageBreakpointService();            
137
            if(!empty($pageBreakpointService) && is_array($dataPagina) && key_exists('id', $dataPagina)) {
138
                $data['page']['breakpoint_file'] = $pageBreakpointService->createPageBreakpoint(
139
                        $dataPagina['id'], $data['columnis.rest.configuration'], $data['breakpoints_hash'], $data['collected_pictures'], $data['columnis.rest.image_sizes_groups']
140
                );
141
            }
142
            
143
            $data['page']['retry'] = $retry;
144
145
            $page->setData($data);
146
            $page->setTemplate($template);
147
        } catch(UnauthorizedException $e) {
148
            $ret = false;
149
            if ($retry >= 0) {
150
                $retry--;
151
                $ret = $this->fetch($page, $params, null, $retry);
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
152
            }
153
            $data = $page->getData();
154
            $data['authorization'] = array(
155
                'error' => $e->getCode(),
156
                'error_description' => $e->getMessage()
157
            );
158
            $page->setData($data);
159
            return $ret;
160
        } catch(ApiRequestException $e) {
161
            return false;
162
        } catch(PathNotFoundException $e) {
163
            throw new PageWithoutTemplateException($e->getMessage(), 0, $e);
164
        } catch(TemplateNameNotSetException $e) {
165
            throw new PageWithoutTemplateException($e->getMessage(), 0, $e);
166
        }
167
        return $response;
168
    }
169
}
170