Completed
Pull Request — master (#165)
by
unknown
03:08
created

Project::addAdAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6667
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
namespace FacebookAds\Object;
26
27
use FacebookAds\Object\Fields\ProjectFields;
28
use FacebookAds\Object\Traits\FieldValidation;
29
use FacebookAds\Http\RequestInterface;
30
use FacebookAds\Cursor;
31
32
class Project extends AbstractCrudObject {
33
  use FieldValidation;
34
35
  /**
36
   * @return string
37
   */
38
  protected function getEndpoint() {
39
    return 'businessprojects';
40
  }
41
42
  /**
43
   * @return ProjectFields
44
   */
45
  public static function getFieldsEnum() {
46
    return ProjectFields::getInstance();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FacebookAds\Obje...tFields::getInstance(); (FacebookAds\Object\Fields\ProjectFields) is incompatible with the return type of the parent method FacebookAds\Object\AbstractObject::getFieldsEnum of type FacebookAds\Enum\EmptyEnum.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
47
  }
48
49
  /**
50
   * @param array $fields
51
   * @param array $params
52
   * @return Cursor
53
   */
54
  public function getPages(
55
    array $fields = array(), array $params = array()) {
56
    return $this->getManyByConnection(
57
      Page::className(), $fields, $params);
58
  }
59
60
  /**
61
   * @param array $fields
62
   * @param array $params
63
   * @return Cursor
64
   */
65
  public function getAdAccounts(
66
    array $fields = array(), array $params = array()) {
67
    return $this->getManyByConnection(
68
      AdAccount::className(), $fields, $params);
69
  }
70
71
  /**
72
   * @param array $fields
73
   * @param array $params
74
   * @return Cursor
75
   */
76
  public function getApps(
77
    array $fields = array(), array $params = array()) {
78
    return $this->getManyByConnection(
79
      App::className(), $fields, $params, 'apps');
80
  }
81
82
  /**
83
   * @param int $page_id
84
   */
85
  public function addPage($page_id) {
86
    $params = array(
87
      'page_id' => $page_id,
88
    );
89
    $this->getApi()->call(
90
      '/'.$this->assureId().'/pages',
91
      RequestInterface::METHOD_POST,
92
      $params);
93
  }
94
95
  /**
96
   * @param int $page_id
97
   */
98
  public function deletePage($page_id) {
99
    $params = array(
100
      'page_id' => $page_id,
101
    );
102
    $this->getApi()->call(
103
      '/'.$this->assureId().'/pages',
104
      RequestInterface::METHOD_DELETE,
105
      $params);
106
  }
107
108
  /**
109
   * @param int $account_id
110
   */
111
  public function addAdAccount($account_id) {
112
    $params = array(
113
      'adaccount_id' => $account_id,
114
    );
115
    $this->getApi()->call(
116
      '/'.$this->assureId().'/adaccounts',
117
      RequestInterface::METHOD_POST,
118
      $params);
119
  }
120
121
  /**
122
   * @param int $account_id
123
   */
124
  public function deleteAdAccount($account_id) {
125
    $params = array(
126
      'adaccount_id' => $account_id,
127
    );
128
    $this->getApi()->call(
129
      '/'.$this->assureId().'/adaccounts',
130
      RequestInterface::METHOD_DELETE,
131
      $params);
132
  }
133
134
  /**
135
   * @param int $app_id
136
   */
137
  public function addApp($app_id) {
138
    $params = array(
139
      'app_id' => $app_id,
140
    );
141
    $this->getApi()->call(
142
      '/'.$this->assureId().'/apps',
143
      RequestInterface::METHOD_POST,
144
      $params);
145
  }
146
147
  /**
148
   * @param int $app_id
149
   */
150
  public function deleteApp($app_id) {
151
    $params = array(
152
      'app_id' => $app_id,
153
    );
154
    $this->getApi()->call(
155
      '/'.$this->assureId().'/apps',
156
      RequestInterface::METHOD_DELETE,
157
      $params);
158
  }
159
}
160