CardQuery   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 23.94 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 17
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A deactivate() 0 19 4
A get() 17 17 4

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
 * Created by Carl Owens ([email protected])
4
 * Company: PartFire Ltd (www.partfire.co.uk)
5
 * Copyright © 2017 PartFire Ltd. All rights reserved.
6
 *
7
 * User:    Carl Owens
8
 * Date:    18/01/2017
9
 * Time:    23:53
10
 * File:    CardQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
use MangoPay\Card as mangoCard;
16
use PartFire\MangoPayBundle\Models\CardQueryInterface;
17
use PartFire\MangoPayBundle\Models\DTOs\Card;
18
use MangoPay\MangoPayApi;
19
use PartFire\MangoPayBundle\Models\DTOs\Translators\CardTranslator;
20
use Symfony\Bridge\Monolog\Logger;
21
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
22
use MangoPay\Libraries\Exception;
23
use MangoPay\Libraries\ResponseException;
24
25
class CardQuery extends AbstractQuery implements CardQueryInterface
26
{
27
    /**
28
     * @var CardTranslator
29
     */
30
    private $cardTranslator;
31
32
    /**
33
     * CardQuery constructor.
34
     * @param $clientId
35
     * @param $clientPassword
36
     * @param $baseUrl
37
     * @param MangoPayApi $mangoPayApi
38
     * @param Logger $logger
39
     * @param CardTranslator $cardTranslator
40
     */
41
    public function __construct(
42
        $clientId,
43
        $clientPassword,
44
        $baseUrl,
45
        MangoPayApi $mangoPayApi,
46
        Logger $logger,
47
        CardTranslator $cardTranslator
48
    ) {
49
        parent::__construct($clientId, $clientPassword, $baseUrl, $mangoPayApi, $logger);
50
        $this->cardTranslator = $cardTranslator;
51
    }
52
53
    /**
54
     * @param string $cardId
55
     * @return null|Card
56
     * @throws PartFireException
57
     */
58 View Code Duplication
    public function get(string $cardId): Card
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60
        try {
61
            $card = $this->mangoPayApi->Cards->Get($cardId);
62
            if ($card instanceof mangoCard) {
63
                return $this->cardTranslator->translateMangoCardToDto($card);
64
            }
65
            $this->logger->addCritical("Card ID: $cardId not found when querying API.");
66
            throw new PartFireException("Card ID: $cardId not found when querying API.");
67
        } catch (ResponseException $e) {
68
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
69
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
70
        } catch (Exception $e) {
71
            $this->logger->addError($e->getMessage());
72
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
73
        }
74
    }
75
76
    public function deactivate(string $cardId): Card
77
    {
78
        try {
79
            $card = $this->mangoPayApi->Cards->Get($cardId);
80
            if ($card instanceof mangoCard) {
81
                $card->Active = false;
82
                $card = $this->mangoPayApi->Cards->Update($card);
83
                return $this->cardTranslator->translateMangoCardToDto($card);
84
            }
85
            $this->logger->addCritical("Card ID: $cardId not found when querying API.");
86
            throw new PartFireException("Card ID: $cardId not found when querying API.");
87
        } catch (ResponseException $e) {
88
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
89
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
90
        } catch (Exception $e) {
91
            $this->logger->addError($e->getMessage());
92
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
93
        }
94
    }
95
}
96