Completed
Push — master ( 90835e...e5901e )
by Oleg
02:50
created

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