Completed
Push — v2 ( 12e20b...6101cd )
by Beñat
03:40
created

AccessTokenSerializer::serialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\StackExchangeApiClient\Serializer;
13
14
use BenatEspina\StackExchangeApiClient\Model\AccessToken;
15
16 View Code Duplication
final class AccessTokenSerializer implements Serializer
0 ignored issues
show
Duplication introduced by
This class 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...
17
{
18
    /**
19
     * The instance itself.
20
     *
21
     * @var self|null
22
     */
23
    private static $instance;
24
25
    /**
26
     * Constructor in a singleton way.
27
     *
28
     * @return self
29
     */
30
    public static function instance()
31
    {
32
        if (null === self::$instance) {
33
            self::$instance = new self();
34
        }
35
36
        return self::$instance;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function serialize($data)
43
    {
44
        if (false === array_key_exists('items', $data)) {
45
            throw new \Exception('Given data is incorrect');
46
        }
47
        if (count($data['items']) > 1) {
48
            $answers = [];
49
            foreach ($data['items'] as $item) {
50
                $answers[] = AccessToken::fromJson($item);
51
            }
52
53
            return $answers;
54
        }
55
56
        return AccessToken::fromJson($data['items'][0]);
57
    }
58
59
    /**
60
     * Constructor. Avoids to use the "new"
61
     * statement to instantiate the class.
62
     */
63
    private function __construct()
64
    {
65
    }
66
}
67