BackupTools::getAvailableStorages()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nicolas
5
 * Date: 26/03/17
6
 * Time: 15:31
7
 */
8
9
namespace Devgiants\Service;
10
11
12
use Devgiants\Configuration\ApplicationConfiguration;
13
use Devgiants\Model\StorageInterface;
14
use hanneskod\classtools\Iterator\ClassIterator;
0 ignored issues
show
Bug introduced by
The type hanneskod\classtools\Iterator\ClassIterator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Monolog\Logger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Config\Definition\Exception\Exception;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config...ion\Exception\Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class BackupTools {
21
	/**
22
	 * Return all protocols available, with key type and value full class path
23
	 * @return array $availableProtocols all protocols available, with key type and value full class path
24
	 */
25
	public function getAvailableStorages() {
26
		$availableStorages = [];
27
28
		$finder   = new Finder();
29
		$iterator = new ClassIterator( $finder->in( __DIR__ . "/../Storage" ) );
30
31
		foreach ( $iterator->getClassMap() as $classname => $splFileInfo ) {
32
			// Check all protocols implements StorageInterface
33
			if ( ! in_array( StorageInterface::class, class_implements( $classname ) ) ) {
34
				throw new Exception( "All protocols must implements StorageInterface : $classname is not." );
35
			}
36
			$availableStorages[ call_user_func( "$classname::getType" ) ] = $classname;
37
		}
38
39
		return $availableStorages;
40
	}
41
42
	/**
43
	 * @param array $storageParams
44
	 *
45
	 * @return StorageInterface
46
	 */
47
	public function getStorageByType( array $storageParams ) {
48
		$availableStorages = $this->getAvailableStorages();
49
		// use the required protocol, and raise exception if inexistant
50
		if ( ! isset( $availableStorages[ $storageParams[ ApplicationConfiguration::STORAGE_TYPE ] ] ) ) {
51
			throw new Exception( "Storage \"{$storageParams[ApplicationConfiguration::STORAGE_TYPE]}\" unavailable" );
52
		}
53
54
		/**
55
		 * @var StorageInterface
56
		 */
57
		return new $availableStorages[ $storageParams[ ApplicationConfiguration::STORAGE_TYPE ] ]( $storageParams );
58
59
	}
60
61
	/**
62
	 * @param OutputInterface $output
63
	 * @param Logger $log
64
	 * @param \Exception $e
65
	 */
66
	public function maximumDetailsErrorHandling( OutputInterface $output, Logger $log, \Exception $e ) {
67
		$output->writeln( "<error>Error happened : {$e->getMessage()}</error>" );
68
		$log->addError( "Error", [
69
			'message' => $e->getMessage(),
70
			'code'    => $e->getCode(),
71
			'file'    => $e->getFile(),
72
			'line'    => $e->getLine(),
73
			'stack'   => $e->getTraceAsString()
74
		] );
75
	}
76
}