Passed
Push — master ( d4f0dd...ebfa4f )
by Robin
05:37
created

JustGivingClient::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Konsulting\JustGivingApiSdk;
4
5
use GuzzleHttp\ClientInterface;
6
use Konsulting\JustGivingApiSdk\Exceptions\ClassNotFoundException;
7
use Konsulting\JustGivingApiSdk\ResourceClients\AccountClient;
8
use Konsulting\JustGivingApiSdk\ResourceClients\CampaignClient;
9
use Konsulting\JustGivingApiSdk\ResourceClients\CharityClient;
10
use Konsulting\JustGivingApiSdk\ResourceClients\CountriesClient;
11
use Konsulting\JustGivingApiSdk\ResourceClients\CurrencyClient;
12
use Konsulting\JustGivingApiSdk\ResourceClients\DonationClient;
13
use Konsulting\JustGivingApiSdk\ResourceClients\EventClient;
14
use Konsulting\JustGivingApiSdk\ResourceClients\FundraisingClient;
15
use Konsulting\JustGivingApiSdk\ResourceClients\LeaderboardClient;
16
use Konsulting\JustGivingApiSdk\ResourceClients\OneSearchClient;
17
use Konsulting\JustGivingApiSdk\ResourceClients\ProjectClient;
18
use Konsulting\JustGivingApiSdk\ResourceClients\SearchClient;
19
use Konsulting\JustGivingApiSdk\ResourceClients\SmsClient;
20
use Konsulting\JustGivingApiSdk\ResourceClients\TeamClient;
21
22
/**
23
 * Class JustGivingClient
24
 *
25
 * @property AccountClient     account
26
 * @property CampaignClient    campaign
27
 * @property CharityClient     charity
28
 * @property CountriesClient   countries
29
 * @property CurrencyClient    currency
30
 * @property DonationClient    donation
31
 * @property EventClient       event
32
 * @property LeaderboardClient leaderboard
33
 * @property OneSearchClient   oneSearch
34
 * @property FundraisingClient fundraising
35
 * @property ProjectClient     project
36
 * @property SearchClient      search
37
 * @property SmsClient         sms
38
 * @property TeamClient        team
39
 * @property AccountClient     Account
40
 * @property CampaignClient    Campaign
41
 * @property CharityClient     Charity
42
 * @property CountriesClient   Countries
43
 * @property CurrencyClient    Currency
44
 * @property DonationClient    Donation
45
 * @property EventClient       Event
46
 * @property LeaderboardClient Leaderboard
47
 * @property ProjectClient     Project
48
 * @property SearchClient      Search
49
 * @property SmsClient         Sms
50
 * @property TeamClient        Team
51
 */
52
class JustGivingClient
53
{
54
    /**
55
     * The clients that have been instantiated.
56
     *
57
     * @var array
58
     */
59
    protected $clients = [];
60
61
    /**
62
     * The client to execute the HTTP requests.
63
     *
64
     * @var ClientInterface
65
     */
66
    protected $httpClient;
67
68
    /**
69
     * JustGivingClient constructor.
70
     *
71
     * @param ClientInterface $client
72
     */
73 85
    public function __construct($client)
74
    {
75 85
        $this->httpClient = $client;
76 85
    }
77
78
    /**
79
     * Allow API classes to be called as properties. Return a singleton client class.
80
     *
81
     * @param string $property
82
     * @return mixed
83
     * @throws \Exception
84
     */
85 73
    public function __get($property)
86
    {
87 73
        $class = __NAMESPACE__ . '\\ResourceClients\\' . ucfirst($property) . 'Client';
88
89 73
        if ( ! class_exists($class)) {
90 1
            throw new ClassNotFoundException($class);
91
        }
92
93 72
        $this->clients[$class] = isset($this->clients[$class])
94 19
            ? $this->clients[$class]
95 72
            : new $class($this->httpClient, $this);
96
97 72
        return $this->clients[$class];
98
    }
99
}
100