GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ImportEventStore   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 18 3
A truncate() 0 7 1
A readFromStdIn() 0 12 2
A readFromJsonFile() 0 8 2
1
<?php declare(strict_types=1);
2
3
namespace SmoothPhp\LaravelAdapter\Console;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Config\Repository;
7
use Illuminate\Database\DatabaseManager;
8
use SmoothPhp\LaravelAdapter\Exception\EventStoreFileNotFound;
9
10
/**
11
 * Class ImportEventStore
12
 * @package SmoothPhp\LaravelAdapter\Console
13
 * @author Simon Bennett <[email protected]>
14
 */
15
final class ImportEventStore extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'smoothphp:import {--file= : If specified, import from a JSON file}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Import EventStore';
30
31
    /** @var DatabaseManager */
32
    private $databaseManager;
33
34
    /** @var Repository */
35
    private $config;
36
37
    /**
38
     * ExportEventStore constructor.
39
     * @param DatabaseManager $databaseManager
40
     * @param Repository $config
41
     */
42
    public function __construct(DatabaseManager $databaseManager, Repository $config)
43
    {
44
        $this->databaseManager = $databaseManager;
45
        parent::__construct();
46
        $this->config = $config;
47
    }
48
49
    /**
50
     * Execute the console command.
51
     *
52
     * @return mixed
53
     */
54
    public function handle()
55
    {
56
        $events = json_decode(!!$this->option('file') ? $this->readFromJsonFile() : $this->readFromStdIn(), true);
57
58
        $this->truncate();
59
60
        foreach ($events as $event) {
61
            $this->databaseManager
62
                ->connection($this->config->get('cqrses.eventstore_connection'))
63
                ->table($this->config->get('cqrses.eventstore_table'))
64
                ->insert($event);
65
        }
66
67
        $this->call('smoothphp:rebuild');
68
69
        return $this->line('Success');
70
71
    }
72
73
    /**
74
     *  Truncate (empty) event store table
75
     */
76
    private function truncate()
77
    {
78
        $this->databaseManager
79
            ->connection($this->config->get('cqrses.eventstore_connection'))
80
            ->table($this->config->get('cqrses.eventstore_table'))
81
            ->truncate();
82
    }
83
84
    /**
85
     * Read event store JSON from std in
86
     *
87
     * @return string
88
     */
89
    private function readFromStdIn()
90
    {
91
        $fd = fopen("php://stdin", "r");
92
        $eventsJson = "";
93
        while (!feof($fd)) {
94
            $eventsJson .= fread($fd, 1024);
95
        }
96
97
        fclose($fd);
98
99
        return $eventsJson;
100
    }
101
102
    /**
103
     * Read event store JSON from file
104
     *
105
     * @return string
106
     */
107
    private function readFromJsonFile()
108
    {
109
        if (!file_exists($path = $this->option('file'))) {
110
            throw new EventStoreFileNotFound(sprintf('%s/%s', __DIR__, $path));
111
        }
112
113
        return file_get_contents($path);
114
    }
115
}
116