BaseCommand   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 94
Duplicated Lines 5.32 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 0
Metric Value
dl 5
loc 94
rs 9.1666
c 0
b 0
f 0
wmc 2
lcom 0
cbo 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
A echoBox() 0 7 1
A setupContainer() 5 70 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
/**
3
 * Anime List Client
4
 *
5
 * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     AnimeListClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2017  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     4.0
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient\Command;
18
19
use function Aviat\AnimeClient\loadToml;
20
21
use Aura\Session\SessionFactory;
22
use Aviat\AnimeClient\{
23
	AnimeClient,
24
	Model,
25
	Util
26
};
27
use Aviat\AnimeClient\API\{
28
	CacheTrait,
29
	Kitsu,
30
	MAL
31
};
32
use Aviat\Banker\Pool;
33
use Aviat\Ion\Config;
34
use Aviat\Ion\Di\{Container, ContainerAware};
35
use ConsoleKit\Command;
36
use ConsoleKit\Widgets\Box;
37
use Monolog\Handler\NullHandler;
38
use Monolog\Logger;
39
40
/**
41
 * Base class for console command setup
42
 */
43
class BaseCommand extends Command {
44
	use CacheTrait;
45
	use ContainerAware;
46
47
	/**
48
	 * Echo text in a box
49
	 *
50
	 * @param string $message
51
	 * @return void
52
	 */
53
	protected function echoBox($message)
54
	{
55
		echo "\n";
56
		$box = new Box($this->getConsole(), $message);
57
		$box->write();
58
		echo "\n";
59
	}
60
61
	/**
62
	 * Setup the Di container
63
	 *
64
	 * @return Container
65
	 */
66
	protected function setupContainer()
67
	{
68
		$APP_DIR = realpath(__DIR__ . '/../../app');
69
		$APPCONF_DIR = realpath("{$APP_DIR}/appConf/");
70
		$CONF_DIR = realpath("{$APP_DIR}/config/");
71
		require_once $APPCONF_DIR . '/base_config.php'; // $base_config
72
73
		$config = loadToml($CONF_DIR);
74
		$config_array = array_merge($base_config, $config);
0 ignored issues
show
Bug introduced by
The variable $base_config does not exist. Did you mean $config?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
75
76
		$di = function ($config_array) use ($APP_DIR) {
77
			$container = new Container();
78
79
			// -------------------------------------------------------------------------
80
			// Logging
81
			// -------------------------------------------------------------------------
82
83
			$app_logger = new Logger('animeclient');
84
			$app_logger->pushHandler(new NullHandler);
85
			$kitsu_request_logger = new Logger('kitsu-request');
86
			$kitsu_request_logger->pushHandler(new NullHandler);
87
			$mal_request_logger = new Logger('mal-request');
88
			$mal_request_logger->pushHandler(new NullHandler);
89
			$container->setLogger($app_logger, 'default');
90
			$container->setLogger($kitsu_request_logger, 'kitsu-request');
91
			$container->setLogger($mal_request_logger, 'mal-request');
92
93
			// Create Config Object
94
			$container->set('config', function() use ($config_array) {
95
				return new Config($config_array);
96
			});
97
98
			// Create Cache Object
99 View Code Duplication
			$container->set('cache', function($container) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
				$logger = $container->getLogger();
101
				$config = $container->get('config')->get('cache');
102
				return new Pool($config, $logger);
103
			});
104
105
			// Create session Object
106
			$container->set('session', function() {
107
				return (new SessionFactory())->newInstance($_COOKIE);
108
			});
109
110
			// Models
111
			$container->set('kitsu-model', function($container) {
112
				$listItem = new Kitsu\ListItem();
113
				$listItem->setContainer($container);
114
				$model = new Kitsu\Model($listItem);
115
				$model->setContainer($container);
116
				$cache = $container->get('cache');
117
				$model->setCache($cache);
118
				return $model;
119
			});
120
			$container->set('mal-model', function($container) {
121
				$listItem = new MAL\ListItem();
122
				$listItem->setContainer($container);
123
				$model = new MAL\Model($listItem);
124
				$model->setContainer($container);
125
				return $model;
126
			});
127
			$container->set('util', function($container) {
128
				return new Util($container);
129
			});
130
131
			return $container;
132
		};
133
134
		return $di($config_array);
135
	}
136
}