Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#924)
by
unknown
03:42
created

DiContainer::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Smr\Container;
4
5
use DI\Container;
6
use DI\ContainerBuilder;
7
use Dotenv\Dotenv;
8
use MySqlDatabase;
9
use mysqli;
10
use RuntimeException;
11
use Smr\MySqlProperties;
12
use function DI\autowire;
13
14
/**
15
 * Class DiContainer
16
 * A wrapper around the DI\Container functionality that will allow
17
 * static usage of container methods.
18
 * @package Smr\Container
19
 */
20
class DiContainer {
21
	private static DiContainer $instance;
22
	private Container $container;
23
24
	private function __construct() {
25
		self::$instance = $this;
26
		$this->container = $this->buildContainer();
27
	}
28
29
	private function getDefinitions(): array {
30
		return [
31
			/*
32
			 * mysqli is a 3rd party library, and we do not have control over its constructor.
33
			 * In order for PHP-DI to construct an instance of a class, each constructor argument must
34
			 * be able to be constructed.
35
			 * PHP-DI cannot construct mysqli by itself, because all of its arguments are primitive types.
36
			 * Therefore, we need to declare a provider factory for the container to use when constructing new instances.
37
			 *
38
			 * The factories themselves are able to use dependency injection as well, so we can provide the MysqlProperties
39
			 * typehint to make sure the container constructs and instance and provides it to the factory.
40
			 */
41
			mysqli::class => function (MySqlProperties $mysqlProperties): mysqli {
42
				// Set the mysqli driver to raise exceptions on errors
43
				if (!mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)) {
44
					throw new RuntimeException('Failed to enable mysqli error reporting');
45
				}
46
				return new mysqli(
47
					$mysqlProperties->getHost(),
48
					$mysqlProperties->getUser(),
49
					$mysqlProperties->getPassword(),
50
					$mysqlProperties->getDatabaseName());
51
			},
52
			Dotenv::class => function (): Dotenv {
53
				return Dotenv::createArrayBacked(ROOT);
54
			},
55
			// Explicitly name all classes that are autowired, so we can take advantage of
56
			// the compiled container feature for a performance boost
57
			MySqlProperties::class => autowire(),
58
			MySqlDatabase::class => autowire()
59
		];
60
	}
61
62
	private function buildContainer(): Container {
63
		$builder = new ContainerBuilder();
64
		return $builder
65
			->addDefinitions($this->getDefinitions())
66
			->useAnnotations(false)
67
			->useAutowiring(true)
68
			// The CompiledContainer.php will be saved to the /tmp directory on the Docker container once
69
			// during its lifecycle (first request)
70
			->enableCompilation("/tmp")
71
			->build();
72
	}
73
74
	/**
75
	 * Create a new DI\Container instance.
76
	 * This needs to be done once during a bootstrapping script, like htdocs/config.inc
77
	 */
78
	public static function initializeContainer(): void {
79
		new DiContainer();
80
	}
81
82
	/**
83
	 * Retrieve the managed instance of $className, or construct a new instance with all dependencies.
84
	 * @param string $className The name of the class to retrieve from the container.
85
	 * @return mixed
86
	 * @throws \DI\DependencyException
87
	 * @throws \DI\NotFoundException
88
	 */
89
	public static function get(string $className) {
90
		return self::getContainer()->get($className);
91
	}
92
93
	/**
94
	 * Construct a fresh instance of $className. Dependencies will be retrieved from the container if they
95
	 * are already managed, and created themselves if they are not.
96
	 * @param string $className The name of the class to construct.
97
	 * @return mixed
98
	 * @throws \DI\DependencyException
99
	 * @throws \DI\NotFoundException
100
	 */
101
	public static function make(string $className) {
102
		return self::getContainer()->make($className);
103
	}
104
105
	/**
106
	 * Return the raw dependency injection Container instance for more robust
107
	 * container management operations.
108
	 * @return Container
109
	 */
110
	public static function getContainer(): Container {
111
		return self::$instance->container;
112
	}
113
}
114