UserQuery::createLegal()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
/**
3
 * Created by Carl Owens ([email protected])
4
 * Company: PartFire Ltd (www.partfire.co.uk)
5
 * Copyright © 2016 PartFire Ltd. All rights reserved.
6
 *
7
 * User:    Carl Owens
8
 * Date:    27/11/2016
9
 * Time:    21:11
10
 * File:    UserQuery.php
11
 **/
12
13
namespace PartFire\MangoPayBundle\Models\Adapters;
14
15
use MangoPay\Libraries\Exception;
16
use MangoPay\Libraries\ResponseException;
17
use MangoPay\MangoPayApi;
18
use PartFire\MangoPayBundle\Models\DTOs\Translators\UserTranslator;
19
use PartFire\MangoPayBundle\Models\DTOs\User;
20
use PartFire\MangoPayBundle\Models\DTOs\UserBase;
21
use PartFire\MangoPayBundle\Models\DTOs\UserLegal as PFUserLegal;
22
use PartFire\MangoPayBundle\Models\DTOs\UserNatural as PFUserNatural;
23
use PartFire\MangoPayBundle\Models\Exception as PartFireException;
24
use PartFire\MangoPayBundle\Models\UserQueryInterface;
25
use Symfony\Bridge\Monolog\Logger;
26
27
class UserQuery extends AbstractQuery implements UserQueryInterface
28
{
29
    /**
30
     * @var UserTranslator
31
     */
32
    protected $userTranslator;
33
34
    /**
35
     * UserQuery constructor.
36
     * @param $clientId
37
     * @param $clientPassword
38
     * @param $baseUrl
39
     * @param MangoPayApi $mangoPayApi
40
     * @param Logger $logger
41
     * @param UserTranslator $userTranslator
42
     */
43
    public function __construct(
44
        $clientId,
45
        $clientPassword,
46
        $baseUrl,
47
        MangoPayApi $mangoPayApi,
48
        Logger $logger,
49
        UserTranslator $userTranslator
50
    ) {
51
        parent::__construct($clientId, $clientPassword, $baseUrl,$mangoPayApi, $logger);
52
        $this->userTranslator = $userTranslator;
53
    }
54
55
    public function createNatural(PFUserNatural $userDto)
56
    {
57
        $mangoUser = $this->userTranslator->convertDTOToMangoPayNaturalUser($userDto);
58
        try {
59
            $mangoUser = $this->mangoPayApi->Users->Create($mangoUser);
60
        } catch (ResponseException $e) {
61
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
62
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
63
        } catch (Exception $e) {
64
            $this->logger->addError($e->getMessage());
65
            throw new PartFireException($e->getMessage(), $e->getCode(), $e);
66
        }
67
        return $this->userTranslator->convertMangoPayNaturalUserToDTO($mangoUser);
68
    }
69
70
    public function createLegal(PFUserLegal $userDto)
71
    {
72
        $mangoUser = $this->userTranslator->convertDTOToMangoPayLegalUser($userDto);
73
        try {
74
            $mangoUser = $this->mangoPayApi->Users->Create($mangoUser);
75
        } catch (ResponseException $e) {
76
            $this->logger->addCritical($e->getMessage(), ['code' => $e->getCode(), 'details' => $e->GetErrorDetails()]);
77
            throw new PartFireException($e->getMessage() . " - " . $e->GetErrorDetails(), $e->getCode(), $e);
78
        } catch (Exception $e) {
79
            $this->logger->addError($e->getMessage());
80
            throw new PartFireException($e->getMessage() . " - " . $e->GetErrorDetails(), $e->getCode(), $e);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class MangoPay\Libraries\Exception as the method GetErrorDetails() does only exist in the following sub-classes of MangoPay\Libraries\Exception: MangoPay\Libraries\ResponseException. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
81
        }
82
        return $this->userTranslator->convertMangoPayLegalUserToDTO($mangoUser);
83
    }
84
85
    public function get($userId)
86
    {
87
        // TODO: Implement get() method.
88
    }
89
90
    public function getAll()
91
    {
92
        // TODO: Implement getAll() method.
93
    }
94
95
    public function update(UserBase $userDto)
96
    {
97
        // TODO: Implement update() method.
98
    }
99
100
    public function delete($userId)
101
    {
102
        // TODO: Implement delete() method.
103
    }
104
}
105