Processor   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 18.18%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 83
ccs 8
cts 44
cp 0.1818
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 22 5
A flush() 0 3 1
A getName() 0 4 1
C getApi() 0 30 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen\Stripe;
6
7
use Shapin\Datagen\FixtureInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Shapin\Datagen\Stripe\FixtureInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Shapin\Datagen\FixtureInterface as the method getObjectName() does only exist in the following implementations of said interface: Shapin\Datagen\Stripe\Fixture.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Shapin\Datagen\FixtureInterface as the method getObjectName() does only exist in the following implementations of said interface: Shapin\Datagen\Stripe\Fixture.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
95
        }
96
    }
97
}
98