Completed
Push — master ( cf8736...406826 )
by
unknown
12:32
created

CustomAudienceMultiKey::addUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 10
nc 1
nop 4
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\Http\RequestInterface;
28
use FacebookAds\Object\Values\CustomAudienceTypes;
29
use FacebookAds\Object\Fields\CustomAudienceFields;
30
use FacebookAds\Object\CustomAudienceNormalizers\HashNormalizer;
31
use FacebookAds\Object\Fields\CustomAudienceMultikeySchemaFields;
32
33
class CustomAudienceMultiKey extends AbstractCrudObject {
34
35
  /**
36
   * @var string
37
   */
38
  const HASH_TYPE_SHA256 = 'sha256';
39
40
  /**
41
  * @var \ArrayObject
42
  */
43
  protected $normalizers;
44
45
  /**
46
   * @return string
47
   */
48
  protected function getEndpoint() {
49
    return 'customaudiences';
50
  }
51
52
  /**
53
   * @return CustomAudienceFields
54
   */
55
  public static function getFieldsEnum() {
56
    return CustomAudienceFields::getInstance();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \FacebookAds\Obje...eFields::getInstance(); (FacebookAds\Object\Fields\CustomAudienceFields) 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...
57
  }
58
59
  /**
60
   * @return \ArrayObject
61
   */
62
  public function getNormalizers() {
63
    if ($this->normalizers === null) {
64
      $this->normalizers = new \ArrayObject(array(
65
        new CustomAudienceNormalizers\EmailNormalizer(),
66
        new CustomAudienceNormalizers\PhoneNormalizer(),
67
        new CustomAudienceNormalizers\MadidNormalizer(),
68
        new CustomAudienceNormalizers\GenderNormalizer(),
69
        new CustomAudienceNormalizers\BirthYearNormalizer(),
70
        new CustomAudienceNormalizers\DateNormalizer(),
71
        new CustomAudienceNormalizers\FirstNameNormalizer(),
72
        new CustomAudienceNormalizers\LastNameNormalizer(),
73
        new CustomAudienceNormalizers\FirstNameInitialNormalizer(),
74
        new CustomAudienceNormalizers\StateNormalizer(),
75
        new CustomAudienceNormalizers\CityNormalizer(),
76
        new CustomAudienceNormalizers\ZipNormalizer(),
77
        new CustomAudienceNormalizers\CountryNormalizer(),
78
      ));
79
    }
80
    return $this->normalizers;
81
  }
82
83
  /**
84
   * Add users to the AdCustomAudiences. There is no max on the total number of
85
   * users that can be added to an audience, but up to 10000 users can be added
86
   * at a given time.
87
   *
88
   * @param array $users
89
   * @param array $types
90
   * @param bool $is_hashed
91
   * @param bool $is_normalized
92
   * @return array
93
   */
94
  public function addUsers(
95
    array $users,
96
    array $types,
97
    $is_hashed = false,
98
    $is_normalized = false) {
99
100
    $params = $this->formatParams($users, $types, $is_hashed, $is_normalized);
101
    return $this->getApi()->call(
102
      '/'.$this->assureId().'/users',
103
      RequestInterface::METHOD_POST,
104
      $params)->getContent();
105
  }
106
107
  /**
108
   * Delete users from AdCustomAudiences
109
   *
110
   * @param array $users
111
   * @param array $types
112
   * @param bool $is_hashed
113
   * @param bool $is_normalized
114
   * @return array
115
   */
116
  public function removeUsers(
117
    array $users,
118
    array $types,
119
    $is_hashed = false,
120
    $is_normalized = false) {
121
122
    $params = $this->formatParams($users, $types, $is_hashed, $is_normalized);
123
    return $this->getApi()->call(
124
      '/'.$this->assureId().'/users',
125
      RequestInterface::METHOD_DELETE,
126
      $params)->getContent();
127
  }
128
129
  /**
130
   * Take users and format them correctly for the request
131
   *
132
   * @param array $users
133
   * @param array $types
134
   * @param bool $is_hashed
135
   * @param bool $is_normalized
136
   * @return array
137
   */
138
  protected function formatParams(
139
    array $users,
140
    array $types,
141
    $is_hashed = false,
142
    $is_normalized = false) {
143
144
    if (!$is_hashed) {
145
      if ($is_normalized) {
146
        $normalizers = new \ArrayObject(array(
147
          new HashNormalizer()
148
        ));
149
      }
150
      else {
151
        $normalizers = clone $this->getNormalizers();
152
        $normalizers->append(new HashNormalizer());
153
      }
154
      foreach ($users as &$user) {
155
        if (count($types) != count($user)) {
156
          throw new \InvalidArgumentException(
157
            "Number of keys in each list in the data should ".
158
            "match the number of keys specified in scheme");
159
          break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
160
        }
161
        foreach ($user as $index => &$key_value) {
162
          $key = $types[$index];
163
          foreach ($normalizers as $normalizer) {
164
            if ($key !== CustomAudienceMultikeySchemaFields::EXTERN_ID &&
165
                $normalizer->shouldNormalize($key, $key_value)) {
166
              $key_value = $normalizer->normalize($key, $key_value);
167
            }
168
          }
169
        }
170
      }
171
    }
172
173
    $payload = array(
174
      'schema' => $types,
175
      'is_raw' => true,
176
      'data' => $users,
177
    );
178
179
    return array('payload' => $payload);
180
  }
181
}
182