Completed
Push — master ( c23590...8ff253 )
by
unknown
02:59
created

CustomAudienceMultiKey::getNormalizers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 2
eloc 16
nc 2
nop 0
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
      ));
78
    }
79
    return $this->normalizers;
80
  }
81
82
  /**
83
   * Add users to the AdCustomAudiences. There is no max on the total number of
84
   * users that can be added to an audience, but up to 10000 users can be added
85
   * at a given time.
86
   *
87
   * @param array $users
88
   * @param array $types
89
   * @param bool $is_hashed
90
   * @param bool $is_normalized
91
   * @return array
92
   */
93
  public function addUsers(
94
    array $users,
95
    array $types,
96
    $is_hashed = false,
97
    $is_normalized = false) {
98
99
    $params = $this->formatParams($users, $types, $is_hashed, $is_normalized);
100
    return $this->getApi()->call(
101
      '/'.$this->assureId().'/users',
102
      RequestInterface::METHOD_POST,
103
      $params)->getContent();
104
  }
105
106
  /**
107
   * Delete users from AdCustomAudiences
108
   *
109
   * @param array $users
110
   * @param array $types
111
   * @param bool $is_hashed
112
   * @param bool $is_normalized
113
   * @return array
114
   */
115
  public function removeUsers(
116
    array $users,
117
    array $types,
118
    $is_hashed = false,
119
    $is_normalized = false) {
120
121
    $params = $this->formatParams($users, $types, $is_hashed, $is_normalized);
122
    return $this->getApi()->call(
123
      '/'.$this->assureId().'/users',
124
      RequestInterface::METHOD_DELETE,
125
      $params)->getContent();
126
  }
127
128
  /**
129
   * Take users and format them correctly for the request
130
   *
131
   * @param array $users
132
   * @param array $types
133
   * @param bool $is_hashed
134
   * @param bool $is_normalized
135
   * @return array
136
   */
137
  protected function formatParams(
138
    array $users,
139
    array $types,
140
    $is_hashed = false,
141
    $is_normalized = false) {
142
143
    if (!$is_hashed) {
144
      if ($is_normalized) {
145
        $normalizers = new \ArrayObject(array(
146
          new HashNormalizer()
147
        ));
148
      }
149
      else {
150
        $normalizers = clone $this->getNormalizers();
151
        $normalizers->append(new HashNormalizer());
152
      }
153
      foreach ($users as &$user) {
154
        if (count($types) != count($user)) {
155
          throw new \InvalidArgumentException(
156
            "Number of keys in each list in the data should ".
157
            "match the number of keys specified in scheme");
158
          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...
159
        }
160
        foreach ($user as $index => &$key_value) {
161
          foreach ($normalizers as $normalizer) {
162
            if ($normalizer->shouldNormalize($types[$index], $key_value)) {
163
              $key_value = $normalizer->normalize($types[$index], $key_value);
164
            }
165
          }
166
        }
167
      }
168
    }
169
170
    $payload = array(
171
      'schema' => $types,
172
      'is_raw' => true,
173
      'data' => $users,
174
    );
175
176
    return array('payload' => $payload);
177
  }
178
}
179