Code Duplication    Length = 104-105 lines in 2 locations

src/Commands/SnapshotCommand.php 1 location

@@ 24-127 (lines=104) @@
21
/**
22
 * Command to take a record snapshot
23
 */
24
class SnapshotCommand extends AbstractCommand
25
{
26
	/**
27
	 * The default command name
28
	 *
29
	 * @var  string|null
30
	 */
31
	protected static $defaultName = 'snapshot';
32
33
	/**
34
	 * JSON view for displaying the statistics.
35
	 *
36
	 * @var  StatsJsonView
37
	 */
38
	private $view;
39
40
	/**
41
	 * Filesystem adapter for the snapshots space.
42
	 *
43
	 * @var  Filesystem
44
	 */
45
	private $filesystem;
46
47
	/**
48
	 * Constructor.
49
	 *
50
	 * @param   StatsJsonView  $view        JSON view for displaying the statistics.
51
	 * @param   Filesystem     $filesystem  Filesystem adapter for the snapshots space.
52
	 */
53
	public function __construct(StatsJsonView $view, Filesystem $filesystem)
54
	{
55
		$this->view       = $view;
56
		$this->filesystem = $filesystem;
57
58
		parent::__construct();
59
	}
60
61
	/**
62
	 * Internal function to execute the command.
63
	 *
64
	 * @param   InputInterface   $input   The input to inject into the command.
65
	 * @param   OutputInterface  $output  The output to inject into the command.
66
	 *
67
	 * @return  integer  The command exit code
68
	 */
69
	protected function doExecute(InputInterface $input, OutputInterface $output): int
70
	{
71
		$symfonyStyle = new SymfonyStyle($input, $output);
72
73
		$symfonyStyle->title('Creating Statistics Snapshot');
74
75
		// We want the full raw data set for our snapshot
76
		$this->view->isAuthorizedRaw(true);
77
78
		$source = $input->getOption('source');
79
80
		$filename = date('YmdHis');
81
82
		if ($source)
83
		{
84
			if (!\in_array($source, StatisticsRepository::ALLOWED_SOURCES))
85
			{
86
				throw new InvalidOptionException(
87
					\sprintf(
88
						'Invalid source "%s" given, valid options are: %s',
89
						$source,
90
						implode(', ', StatisticsRepository::ALLOWED_SOURCES)
91
					)
92
				);
93
			}
94
95
			$this->view->setSource($source);
96
97
			$filename .= '_' . $source;
98
		}
99
100
		if (!$this->filesystem->write($filename, $this->view->render()))
101
		{
102
			$symfonyStyle->error('Failed writing snapshot to the filesystem.');
103
104
			return 1;
105
		}
106
107
		$symfonyStyle->success('Snapshot recorded.');
108
109
		return 0;
110
	}
111
112
	/**
113
	 * Configures the current command.
114
	 *
115
	 * @return  void
116
	 */
117
	protected function configure(): void
118
	{
119
		$this->setDescription('Takes a snapshot of the statistics data.');
120
		$this->addOption(
121
			'source',
122
			null,
123
			InputOption::VALUE_OPTIONAL,
124
			'If given, filters the snapshot to a single source.'
125
		);
126
	}
127
}
128

src/Commands/SnapshotRecentlyUpdatedCommand.php 1 location

@@ 24-128 (lines=105) @@
21
/**
22
 * Command to take a record snapshot for recently updated records
23
 */
24
class SnapshotRecentlyUpdatedCommand extends AbstractCommand
25
{
26
	/**
27
	 * The default command name
28
	 *
29
	 * @var  string|null
30
	 */
31
	protected static $defaultName = 'snapshot:recently-updated';
32
33
	/**
34
	 * JSON view for displaying the statistics.
35
	 *
36
	 * @var  StatsJsonView
37
	 */
38
	private $view;
39
40
	/**
41
	 * Filesystem adapter for the snapshots space.
42
	 *
43
	 * @var  Filesystem
44
	 */
45
	private $filesystem;
46
47
	/**
48
	 * Constructor.
49
	 *
50
	 * @param   StatsJsonView  $view        JSON view for displaying the statistics.
51
	 * @param   Filesystem     $filesystem  Filesystem adapter for the snapshots space.
52
	 */
53
	public function __construct(StatsJsonView $view, Filesystem $filesystem)
54
	{
55
		$this->view       = $view;
56
		$this->filesystem = $filesystem;
57
58
		parent::__construct();
59
	}
60
61
	/**
62
	 * Internal function to execute the command.
63
	 *
64
	 * @param   InputInterface   $input   The input to inject into the command.
65
	 * @param   OutputInterface  $output  The output to inject into the command.
66
	 *
67
	 * @return  integer  The command exit code
68
	 */
69
	protected function doExecute(InputInterface $input, OutputInterface $output): int
70
	{
71
		$symfonyStyle = new SymfonyStyle($input, $output);
72
73
		$symfonyStyle->title('Creating Statistics Snapshot');
74
75
		// We want the full raw data set for our snapshot
76
		$this->view->isAuthorizedRaw(true);
77
		$this->view->isRecent(true);
78
79
		$source = $input->getOption('source');
80
81
		$filename = date('YmdHis') . '_recent';
82
83
		if ($source)
84
		{
85
			if (!\in_array($source, StatisticsRepository::ALLOWED_SOURCES))
86
			{
87
				throw new InvalidOptionException(
88
					\sprintf(
89
						'Invalid source "%s" given, valid options are: %s',
90
						$source,
91
						implode(', ', StatisticsRepository::ALLOWED_SOURCES)
92
					)
93
				);
94
			}
95
96
			$this->view->setSource($source);
97
98
			$filename .= '_' . $source;
99
		}
100
101
		if (!$this->filesystem->write($filename, $this->view->render()))
102
		{
103
			$symfonyStyle->error('Failed writing snapshot to the filesystem.');
104
105
			return 1;
106
		}
107
108
		$symfonyStyle->success('Snapshot recorded.');
109
110
		return 0;
111
	}
112
113
	/**
114
	 * Configures the current command.
115
	 *
116
	 * @return  void
117
	 */
118
	protected function configure(): void
119
	{
120
		$this->setDescription('Takes a snapshot of the recently updated statistics data.');
121
		$this->addOption(
122
			'source',
123
			null,
124
			InputOption::VALUE_OPTIONAL,
125
			'If given, filters the snapshot to a single source.'
126
		);
127
	}
128
}
129