Completed
Push — master ( 1d1446...5246d9 )
by Oleg
02:12
created

Pages::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 16
loc 16
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 07 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Service\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Resource\v2\Page;
10
use WebMarketingROI\OptimizelyPHP\Exception;
11
12
/**
13
 * Provides methods for working with Optimizely project pages.
14
 */
15
class Pages
16
{
17
    /**
18
     * Optimizely API Client.
19
     * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient
20
     */
21
    private $client;
22
    
23
    /**
24
     * Constructor.
25
     */
26 5
    public function __construct($client)
27
    {
28 5
        $this->client = $client;
29 5
    }
30
    
31
    /**
32
     * List all Pages for a project.
33
     * @param integer $projectId
34
     * @param integer $page
35
     * @param integer $perPage
36
     * @return Result
37
     * @throws Exception
38
     */
39 1 View Code Duplication
    public function listAll($projectId, $page=1, $perPage=25)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41 1
        if ($page<0) {
42
            throw new Exception('Invalid page number passed');
43
        }
44
        
45 1
        if ($perPage<0) {
46
            throw new Exception('Invalid page size passed');
47
        }
48
        
49 1
        $result = $this->client->sendApiRequest('/pages', 
50
                array(
51 1
                    'project_id'=>$projectId,
52 1
                    'page'=>$page,
53
                    'per_page'=>$perPage
54 1
                ));
55
        
56 1
        $pages = array();
57 1
        foreach ($result->getDecodedJsonData() as $pageInfo) {
58 1
            $page = new Page($pageInfo);
59 1
            $pages[] = $page;
60 1
        }
61 1
        $result->setPayload($pages);
62
        
63 1
        return $result;
64
    }
65
    
66
    /**
67
     * Get metadata for a single Page
68
     * @param integer $pageId
69
     * @return Result
70
     * @throws Exception
71
     */
72 1
    public function get($pageId)
73
    {
74 1
        if (!is_int($pageId)) {
75
            throw new Exception("Integer page ID expected, while got '$pageId'");
76
        }
77
        
78 1
        $result = $this->client->sendApiRequest("/pages/$pageId");
79
        
80 1
        $page = new Page($result->getDecodedJsonData());
81 1
        $result->setPayload($page);
82
        
83 1
        return $result;
84
    }
85
    
86
    /**
87
     * Create a new Page in a provided Project
88
     * @param Page $page
89
     */
90 1 View Code Duplication
    public function create($page)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92 1
        if (!($page instanceOf Page)) {
93
            throw new Exception("Expected argument of type Page");
94
        }
95
        
96 1
        $postData = $page->toArray();
97
        
98 1
        $result = $this->client->sendApiRequest("/pages", array(), 'POST', 
99 1
                $postData);
100
        
101 1
        $page = new Page($result->getDecodedJsonData());
102 1
        $result->setPayload($page);
103
        
104 1
        return $result;
105
    }
106
    
107
    /**
108
     * Update a Page in a provided Project
109
     * @param integer $pageId
110
     * @param Audience $page
111
     * @return Result
112
     * @throws Exception
113
     */
114 1 View Code Duplication
    public function update($pageId, $page) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116 1
        if (!($page instanceOf Page)) {
117
            throw new Exception("Expected argument of type Page");
118
        }
119
        
120 1
        $postData = $page->toArray();
121
                
122 1
        $result = $this->client->sendApiRequest("/pages/$pageId", array(), 'PATCH', 
123 1
                $postData);
124
        
125 1
        $page = new Page($result->getDecodedJsonData());
126 1
        $result->setPayload($page);
127
        
128 1
        return $result;
129
    }
130
    
131
    /**
132
     * Delete a Page within a Project by ID.
133
     * @param integer $pageId
134
     * @return Result
135
     * @throws Exception
136
     */
137 1
    public function delete($pageId) 
138
    {
139 1
        $result = $this->client->sendApiRequest("/pages/$pageId", array(), 'DELETE', 
140 1
                array());
141
        
142 1
        return $result;
143
    }
144
}
145
146
147
148
149
150
151