-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddDataProviderNetwork.php
More file actions
143 lines (118 loc) · 5.07 KB
/
AddDataProviderNetwork.php
File metadata and controls
143 lines (118 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace App\Console\Commands;
use App\Models\DataProviderColl;
use App\Models\DataProviderCollHasTeam;
use App\Models\Team;
use Illuminate\Console\Command;
class AddDataProviderNetwork extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:add-data-provider-network';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Data Provider Network for import, based on file "../storage/migration_files/data_provider_networkv3-Introduction.csv"';
private $csvData = [];
public function __construct()
{
parent::__construct();
$this->readMigrationFile(storage_path() . '/migration_files/data_provider_networkv3-Introduction.csv');
}
/**
* Execute the console command.
*/
public function handle()
{
$askDataProviderNetwork = $this->ask('Data Provider Network for import, based on file "../storage/migration_files/data_provider_networkv3-Introduction.csv"? [default value all]', 'all');
$askInitDataProviderNetwork = $this->ask('Do you want to initialize the database for "Data Provider Network"? yes/no [default value no]', 'no');
if ($askInitDataProviderNetwork === 'yes') {
DataProviderCollHasTeam::truncate();
DataProviderColl::truncate();
}
$csvData = $this->csvData;
if ($askDataProviderNetwork === 'all') {
foreach ($csvData as $item) {
$teamName = strtoupper(trim($item['Data provider/Team']));
$dataProviderName = strtoupper(trim($item['Data provider network']));
$dataProviderSummary = htmlentities(trim($item['Summary']));
$return = $this->dataProviderNetworkTeam($dataProviderName, $teamName, $dataProviderSummary);
if (!$return) {
continue;
}
}
}
if ($askDataProviderNetwork !== 'all') {
$inputDataProviderName = strtoupper(trim($askDataProviderNetwork));
foreach ($csvData as $item) {
$teamName = strtoupper(trim($item['Data provider/Team']));
$dataProviderName = strtoupper(trim($item['Data provider network']));
$dataProviderSummary = strtoupper(trim($item['Summary']));
if ($inputDataProviderName !== $dataProviderName) {
echo 'Found Data Provider Network name ' . $dataProviderName . '. skipping ...' . PHP_EOL;
continue;
}
$return = $this->dataProviderNetworkTeam($dataProviderName, $teamName, $dataProviderSummary);
if (!$return) {
continue;
}
}
}
}
private function dataProviderNetworkTeam(string $dataProviderNetworkName, string $teamName, string $dataProviderSummary = null): bool
{
// check in teams
$team = Team::where('name', $teamName)->first();
if (!$team) {
echo 'Team ' . $teamName . ' not found. skipping ...' . PHP_EOL;
return false;
}
// check if exists DataProviderNetwork and/or create one
$dataProviderNetwork = DataProviderColl::where('name', $dataProviderNetworkName)->withTrashed()->first();
if (!$dataProviderNetwork) {
$dataProviderNetwork = DataProviderColl::create([
'name' => $dataProviderNetworkName,
'enabled' => 1,
'img_url' => null,
'summary' => $dataProviderSummary,
]);
echo 'Data Provider network with name ' . $dataProviderNetworkName . ' was created.' . PHP_EOL;
}
// check if DataProviderNetwork & Team exists
$dataProviderNetworkTeam = DataProviderCollHasTeam::where([
'data_provider_coll_id' => (int) $dataProviderNetwork->id,
'team_id' => (int) $team->id,
])->first();
if ($dataProviderNetworkTeam) {
echo 'The team ' . $teamName . ' found in relation with ' .
$dataProviderNetworkName . '. skipping ...' . PHP_EOL;
return false;
}
// add DataProviderNetwork with Team
$createDataProviderNetworkTeam = DataProviderCollHasTeam::create([
'data_provider_coll_id' => (int) $dataProviderNetwork->id,
'team_id' => (int) $team->id,
]);
echo 'Was created relation between team (' . $teamName . ') and data provider network (' .
$dataProviderNetworkName . '). ID: ' . $createDataProviderNetworkTeam->id . PHP_EOL;
return true;
}
private function readMigrationFile(string $migrationFile): void
{
$file = fopen($migrationFile, 'r');
$headers = fgetcsv($file);
while (($row = fgetcsv($file)) !== false) {
$item = [];
foreach ($row as $key => $value) {
$item[trim($headers[$key], "\xEF\xBB\xBF")] = $value ?: '';
}
$this->csvData[] = $item;
}
fclose($file);
}
}