|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\BatchImport; |
| 4 | + |
| 5 | +use App\Models\Taxonomy; |
| 6 | +use Carbon\Exceptions\UnitException; |
| 7 | +use Exception; |
| 8 | +use GuzzleHttp\Client; |
| 9 | +use GuzzleHttp\Exception\GuzzleException; |
| 10 | +use Illuminate\Database\Eloquent\MassAssignmentException; |
| 11 | +use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 12 | +use Illuminate\Support\Carbon; |
| 13 | +use Illuminate\Support\Facades\App; |
| 14 | +use Illuminate\Support\Facades\DB; |
| 15 | +use Illuminate\Support\Facades\Log; |
| 16 | +use Illuminate\Support\Facades\Schema; |
| 17 | +use RuntimeException; |
| 18 | + |
| 19 | +class OpenActiveTaxonomyImporter |
| 20 | +{ |
| 21 | + /** |
| 22 | + * The URL for the Open Active taxonomy in jsonld format. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $openActiveDirectoryUrl = 'https://openactive.io/activity-list/activity-list.jsonld'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Dry run: no taxonomies will be created |
| 30 | + * |
| 31 | + * @var bool |
| 32 | + **/ |
| 33 | + protected $dryRun = false; |
| 34 | + |
| 35 | + public function __construct($directoryUrl = null, $dryRun = false) |
| 36 | + { |
| 37 | + if ($directoryUrl) { |
| 38 | + $this->openActiveDirectoryUrl = $directoryUrl; |
| 39 | + } |
| 40 | + $this->dryRun = $dryRun; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Fetch the Open Active taxonomy data and store it as a collection. |
| 45 | + * |
| 46 | + * @param mixed $openActiveDirectoryUrl |
| 47 | + * @return array |
| 48 | + */ |
| 49 | + public function fetchTaxonomies($openActiveDirectoryUrl) |
| 50 | + { |
| 51 | + $client = new Client(); |
| 52 | + try { |
| 53 | + $response = $client->get($openActiveDirectoryUrl); |
| 54 | + if (200 === $response->getStatusCode() && $response->getBody()->isReadable()) { |
| 55 | + $data = json_decode((string)$response->getBody(), true); |
| 56 | + |
| 57 | + return $data['concept']; |
| 58 | + } |
| 59 | + } catch (\Exception $e) { |
| 60 | + Log::error($e->getMessage()); |
| 61 | + |
| 62 | + throw $e; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the root Open Active category. |
| 68 | + * |
| 69 | + * @return \App\Models\Taxonomy |
| 70 | + */ |
| 71 | + public function getOpenActiveCategory() |
| 72 | + { |
| 73 | + return Taxonomy::category() |
| 74 | + ->children() |
| 75 | + ->where('name', 'OpenActive') |
| 76 | + ->firstOrFail(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Runs the import process |
| 81 | + * |
| 82 | + * @return void |
| 83 | + * @throws ModelNotFoundException |
| 84 | + * @throws Exception |
| 85 | + * @throws GuzzleException |
| 86 | + * @throws UnitException |
| 87 | + * @throws RuntimeException |
| 88 | + * @throws MassAssignmentException |
| 89 | + * @return int |
| 90 | + */ |
| 91 | + public function runImport():int |
| 92 | + { |
| 93 | + // Get the root Open Active category |
| 94 | + $openActiveCategory = $this->getOpenActiveCategory(); |
| 95 | + |
| 96 | + // Fetch the remote Open Active data |
| 97 | + $openActiveTaxonomies = $this->fetchTaxonomies($this->openActiveDirectoryUrl); |
| 98 | + |
| 99 | + // Correctly format the remote data ready for import |
| 100 | + $taxonomyImports = $this->mapOpenActiveTaxonomyImport($openActiveCategory, $openActiveTaxonomies); |
| 101 | + |
| 102 | + return $this->importTaxonomies($openActiveCategory, $taxonomyImports); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Import the formatted taxonomies into the database. |
| 107 | + * |
| 108 | + * @param \App\Models\Taxonomy $rootTaxonomy |
| 109 | + * @param array $taxonomyImports |
| 110 | + * @return int |
| 111 | + */ |
| 112 | + public function importTaxonomies(Taxonomy $openActiveCategory, array $taxonomyImports): int |
| 113 | + { |
| 114 | + // Import the data |
| 115 | + if (App::environment() != 'testing') { |
| 116 | + DB::beginTransaction(); |
| 117 | + } |
| 118 | + |
| 119 | + Schema::disableForeignKeyConstraints(); |
| 120 | + |
| 121 | + DB::table((new Taxonomy())->getTable())->insertOrIgnore($taxonomyImports); |
| 122 | + |
| 123 | + if (!$this->dryRun && App::environment() != 'testing') { |
| 124 | + DB::commit(); |
| 125 | + } |
| 126 | + |
| 127 | + Schema::enableForeignKeyConstraints(); |
| 128 | + |
| 129 | + $openActiveCategory->refresh(); |
| 130 | + |
| 131 | + $openActiveCategory->updateDepth(); |
| 132 | + |
| 133 | + return count($taxonomyImports); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Map the imported data into an import friendly format. |
| 138 | + * |
| 139 | + * @param \App\Models\Taxonomy $rootTaxonomy |
| 140 | + * @param array openActiveTaxonomyData |
| 141 | + * @param mixed $openActiveTaxonomyData |
| 142 | + * @return array |
| 143 | + */ |
| 144 | + public function mapOpenActiveTaxonomyImport(Taxonomy $rootTaxonomy, $openActiveTaxonomyData) |
| 145 | + { |
| 146 | + $nowDateTimeString = Carbon::now()->toDateTimeString(); |
| 147 | + |
| 148 | + return array_map(function ($taxonomy) use ($rootTaxonomy, $nowDateTimeString) { |
| 149 | + return array_merge( |
| 150 | + $this->mapOpenActiveTaxonomyToTaxonomyModelSchema($rootTaxonomy, $taxonomy), |
| 151 | + [ |
| 152 | + 'created_at' => $nowDateTimeString, |
| 153 | + 'updated_at' => $nowDateTimeString, |
| 154 | + ] |
| 155 | + ); |
| 156 | + }, $openActiveTaxonomyData); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Return the uuid component from an Open Active directory url. |
| 161 | + * |
| 162 | + * @param string $identifierUrl |
| 163 | + * @return string |
| 164 | + */ |
| 165 | + private function parseIdentifier(string $identifierUrl) |
| 166 | + { |
| 167 | + return mb_substr($identifierUrl, mb_strpos($identifierUrl, '#') + 1); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Convert Open Active taxonomies into Taxonomy model data. |
| 172 | + * |
| 173 | + * @param \App\Models\Taxonomy $rootTaxonomy |
| 174 | + * @param array openActiveTaxonomyData |
| 175 | + * @return array |
| 176 | + */ |
| 177 | + private function mapOpenActiveTaxonomyToTaxonomyModelSchema(Taxonomy $rootTaxonomy, array $taxonomyData) |
| 178 | + { |
| 179 | + return [ |
| 180 | + 'id' => $taxonomyData['identifier'], |
| 181 | + 'name' => $taxonomyData['prefLabel'], |
| 182 | + 'parent_id' => array_key_exists('broader', $taxonomyData) ? $this->parseIdentifier($taxonomyData['broader'][0]) : $rootTaxonomy->id, |
| 183 | + 'order' => 0, |
| 184 | + 'depth' => 2, |
| 185 | + ]; |
| 186 | + } |
| 187 | +} |
0 commit comments