Completed
Push — master ( 8b4718...61d59f )
by Oleg
02:27
created

Projects::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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