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); |
|
|
|
|
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
|
|
|
|
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: