Completed
Branch develop (205409)
by Timothy
07:06
created

src/Command/BaseCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Aura\Session\SessionFactory;
20
use Aviat\AnimeClient\{
21
	AnimeClient,
22
	Model,
23
	Util
24
};
25
use Aviat\AnimeClient\API\CacheTrait;
26
use Aviat\AnimeClient\API\Kitsu\{
27
	Auth as KitsuAuth,
28
	ListItem as KitsuListItem,
29
	Model as KitsuModel
30
};
31
use Aviat\AnimeClient\API\MAL\{
32
	ListItem as MALListItem,
33
	Model as MALModel
34
};
35
use Aviat\Banker\Pool;
36
use Aviat\Ion\Config;
37
use Aviat\Ion\Di\{Container, ContainerAware};
38
use ConsoleKit\Command;
39
use ConsoleKit\Widgets\Box;
40
use Monolog\Handler\NullHandler;
41
use Monolog\Logger;
42
43
/**
44
 * Base class for console command setup
45
 */
46
class BaseCommand extends Command {
47
	use CacheTrait;
48
	use ContainerAware;
49
50
	/**
51
	 * Echo text in a box
52
	 *
53
	 * @param string $message
54
	 * @return void
55
	 */
56
	protected function echoBox($message)
57
	{
58
		echo "\n";
59
		$box = new Box($this->getConsole(), $message);
60
		$box->write();
61
		echo "\n";
62
	}
63
64
	/**
65
	 * Setup the Di container
66
	 *
67
	 * @return Container
68
	 */
69
	protected function setupContainer()
70
	{
71
		$APP_DIR = realpath(__DIR__ . '/../../app');
72
		$CONF_DIR = realpath("{$APP_DIR}/config/");
73
		require_once $CONF_DIR . '/base_config.php'; // $base_config
74
75
		$config = AnimeClient::loadToml($CONF_DIR);
76
		$config_array = array_merge($base_config, $config);
77
78
		$di = function ($config_array) use ($APP_DIR) {
79
			$container = new Container();
80
			
81
			// -------------------------------------------------------------------------
82
			// Logging
83
			// -------------------------------------------------------------------------
84
85
			$app_logger = new Logger('animeclient');
86
			$app_logger->pushHandler(new NullHandler);
87
			$request_logger = new Logger('request');
88
			$request_logger->pushHandler(new NullHandler);
89
			$container->setLogger($app_logger, 'default');
90
			$container->setLogger($request_logger, 'request');
91
			
92
			// Create Config Object
93
			$container->set('config', function() use ($config_array) {
94
				return new Config($config_array);
95
			});
96
97
			// Create Cache Object
98 View Code Duplication
			$container->set('cache', function($container) {
99
				$logger = $container->getLogger();
100
				$config = $container->get('config')->get('cache');
101
				return new Pool($config, $logger);
102
			});
103
104
			// Create session Object
105
			$container->set('session', function() {
106
				return (new SessionFactory())->newInstance($_COOKIE);
107
			});
108
109
			// Models
110 View Code Duplication
			$container->set('kitsu-model', function($container) {
0 ignored issues
show
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...
111
				$listItem = new KitsuListItem();
112
				$listItem->setContainer($container);
113
				$model = new KitsuModel($listItem);
114
				$model->setContainer($container);
115
				$cache = $container->get('cache');
116
				$model->setCache($cache);
117
				return $model;
118
			});
119 View Code Duplication
			$container->set('mal-model', function($container) {
0 ignored issues
show
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...
120
				$listItem = new MALListItem();
121
				$listItem->setContainer($container);
122
				$model = new MALModel($listItem);
123
				$model->setContainer($container);
124
				return $model;
125
			});
126
			$container->set('util', function($container) {
127
				return new Util($container);
128
			});
129
130
			return $container;
131
		};
132
133
		return $di($config_array);
134
	}
135
}