1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shapin\Datagen\Stripe; |
6
|
|
|
|
7
|
|
|
use Shapin\Datagen\FixtureInterface; |
|
|
|
|
8
|
|
|
use Shapin\Datagen\ProcessorInterface; |
9
|
|
|
use Shapin\Datagen\ReferenceManager; |
10
|
|
|
use Shapin\Datagen\Stripe\Exception\UnknownObjectException; |
11
|
|
|
use Shapin\Stripe\Api\HttpApi; |
12
|
|
|
use Shapin\Stripe\Exception\Domain\ResourceAlreadyExistsException; |
13
|
|
|
use Shapin\Stripe\StripeClient; |
14
|
|
|
|
15
|
|
|
class Processor implements ProcessorInterface |
16
|
|
|
{ |
17
|
|
|
private $stripeClient; |
18
|
|
|
private $referenceManager; |
19
|
|
|
|
20
|
2 |
|
public function __construct(StripeClient $stripeClient, ReferenceManager $referenceManager) |
21
|
|
|
{ |
22
|
2 |
|
$this->stripeClient = $stripeClient; |
23
|
2 |
|
$this->referenceManager = $referenceManager; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function process(FixtureInterface $fixture, array $options = []): void |
30
|
|
|
{ |
31
|
|
|
if (!$fixture instanceof Fixture) { |
32
|
|
|
throw new \InvalidArgumentException('You must provider an instance of '.Fixture::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$api = $this->getApi($fixture); |
36
|
|
|
|
37
|
|
|
foreach ($fixture->getObjects() as $key => $object) { |
38
|
|
|
$object = $this->referenceManager->findAndReplace($object); |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$object = $api->create($object); |
42
|
|
|
} catch (ResourceAlreadyExistsException $e) { |
43
|
|
|
// Doing nothing for now |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (\is_string($key)) { |
47
|
|
|
$this->referenceManager->add($fixture->getObjectName(), $key, $object); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
2 |
|
public function flush(array $options = []): void |
56
|
|
|
{ |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function getName(): string |
63
|
|
|
{ |
64
|
2 |
|
return 'stripe'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getApi(FixtureInterface $fixture): HttpApi |
68
|
|
|
{ |
69
|
|
|
switch ($fixture->getObjectName()) { |
|
|
|
|
70
|
|
|
case 'account': |
71
|
|
|
return $this->stripeClient->accounts(); |
72
|
|
|
case 'charge': |
73
|
|
|
return $this->stripeClient->charges(); |
74
|
|
|
case 'coupon': |
75
|
|
|
return $this->stripeClient->coupons(); |
76
|
|
|
case 'customer': |
77
|
|
|
return $this->stripeClient->customers(); |
78
|
|
|
case 'plan': |
79
|
|
|
return $this->stripeClient->plans(); |
80
|
|
|
case 'product': |
81
|
|
|
return $this->stripeClient->products(); |
82
|
|
|
case 'refund': |
83
|
|
|
return $this->stripeClient->refunds(); |
84
|
|
|
case 'source': |
85
|
|
|
return $this->stripeClient->sources(); |
86
|
|
|
case 'subscription': |
87
|
|
|
return $this->stripeClient->subscriptions(); |
88
|
|
|
case 'tax_rate': |
89
|
|
|
return $this->stripeClient->taxRates(); |
90
|
|
|
case 'transfer': |
91
|
|
|
return $this->stripeClient->transfers(); |
92
|
|
|
|
93
|
|
|
default: |
94
|
|
|
throw new UnknownObjectException($fixture->getObjectName()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: