Crowdin::__get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Crowdin API implementation in PHP.
4
 *
5
 * @copyright  Copyright (C) 2016 Nikolai Plath (elkuku)
6
 * @license    WTFPL - See license.txt
7
 */
8
9
namespace ElKuKu\Crowdin;
10
11
use GuzzleHttp\Client as HttpClient;
12
13
/**
14
 * Class for interacting with Crowdin.
15
 *
16
 * @property-read  Package\Directory    $directory    Crowdin API object for the Directory package.
17
 * @property-read  Package\File         $file         Crowdin API object for the File package.
18
 * @property-read  Package\Glossary     $glossary     Crowdin API object for the Glossary package.
19
 * @property-read  Package\Language     $language     Crowdin API object for the Language package.
20
 * @property-read  Package\Memory       $memory       Crowdin API object for the Memory package.
21
 * @property-read  Package\Project      $project      Crowdin API object for the Project package.
22
 * @property-read  Package\Translation  $translation  Crowdin API object for the Translation package.
23
 *
24
 * @since  1.0
25
 */
26
class Crowdin
27
{
28
	/**
29
	 * The Crowdin project id.
30
	 * @var string
31
	 */
32
	private $projectId;
33
34
	/**
35
	 * The Crowdin API key.
36
	 * @var string
37
	 */
38
	private $apiKey;
39
40
	/**
41
	 * The HTTP client object.
42
	 * @var HttpClient
43
	 */
44
	private $httpClient;
45
46
	/**
47
	 * Constructor.
48
	 *
49
	 * @param   string      $projectId  The project ID.
50
	 * @param   string      $apiKey     The API key
51
	 * @param   string      $baseUri    The base URI
52
	 * @param   HttpClient  $client     The HTTP client object.
53
	 *
54
	 * @since    1.0
55
	 */
56 1
	public function __construct(string $projectId, string $apiKey, string $baseUri = 'https://api.crowdin.com/api/', HttpClient $client = null)
57
	{
58 1
		$this->projectId = $projectId;
59 1
		$this->apiKey = $apiKey;
60
61 1
		$this->httpClient = $client ?? new HttpClient(['base_uri' => $baseUri]);
62 1
	}
63
64
	/**
65
	 * Magic method to lazily create API objects.
66
	 *
67
	 * @param   string  $name  Name of property to retrieve
68
	 *
69
	 * @return  Object
70
	 *
71
	 * @since   1.0
72
	 * @throws  \InvalidArgumentException If $name is not a valid sub class.
73
	 */
74 2
	public function __get(string $name)
75
	{
76 2
		$class = 'ElKuKu\\Crowdin\\Package\\' . ucfirst($name);
77
78 2
		if (class_exists($class))
79
		{
80 1
			if (false === isset($this->$name))
81
			{
82 1
				$this->$name = new $class($this->projectId, $this->apiKey, $this->httpClient);
83
			}
84
85 1
			return $this->$name;
86
		}
87
88 1
		throw new \InvalidArgumentException(sprintf('Argument %s produced an invalid class name: %s', $name, $class));
89
	}
90
}
91