Completed
Pull Request — master (#249)
by David
10:15
created

IndexResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 60
loc 60
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 18 18 4
A __construct() 5 5 1
A getTotalCount() 4 4 1
A getRoutes() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Resource\Api\Routes\Response;
11
12
use Mailgun\Resource\Api\Routes\Dto\RouteDto;
13
use Mailgun\Resource\ApiResponse;
14
15
/**
16
 * @author David Garcia <[email protected]>
17
 */
18 View Code Duplication
final class IndexResponse implements ApiResponse
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...
19
{
20
    /**
21
     * @var int
22
     */
23
    private $totalCount;
24
25
    /**
26
     * @var RouteDto[]
27
     */
28
    private $items;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public static function create(array $data)
34
    {
35
        $items = [];
36
37
        if (isset($data['items'])) {
38
            foreach ($data['items'] as $item) {
39
                $items[] = RouteDto::create($item);
40
            }
41
        }
42
43
        if (isset($data['total_count'])) {
44
            $count = $data['total_count'];
45
        } else {
46
            $count = count($items);
47
        }
48
49
        return new self($count, $items);
50
    }
51
52
    /**
53
     * @param int        $totalCount
54
     * @param RouteDto[] $items
55
     */
56
    private function __construct($totalCount, array $items)
57
    {
58
        $this->totalCount = $totalCount;
59
        $this->items = $items;
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getTotalCount()
66
    {
67
        return $this->totalCount;
68
    }
69
70
    /**
71
     * @return RouteDto[]
72
     */
73
    public function getRoutes()
74
    {
75
        return $this->items;
76
    }
77
}
78