Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions app/Classes/Repositories/KeyMessageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Classes\Repositories;

use App\Models\KeyMessage;


class KeyMessageRepository implements KeyMessageRepositoryInterface
{

/**
* @var KeyMessage
*/
protected $keyMessageModel;

/**
* @param KeyMessage $keyMessageModel
*/
public function __construct(KeyMessage $keyMessageModel)
{
$this->keyMessageModel = $keyMessageModel;
}

/**
* @param array $attributes
* @return static
*/
public function newInstance(array $attributes = [])
{
return $this->keyMessageModel->newInstance($attributes);
}

/**
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function all($columns = ['*'])
{
return $this->keyMessageModel->all($columns);
}

/**
* @param array $attributes
* @return static
*/
public function create(array $attributes)
{
return $this->keyMessageModel->create($attributes);
}

/**
* @param $id
* @param array $columns
* @return mixed
*/
public function find($id, $columns = ['*'])
{
return $this->keyMessageModel->findOrFail($id, $columns);
}

/**
* @param $id
* @return int
*/
public function destroy($id)
{
return $this->keyMessageModel->destroy($id);
}

/**
* @param $id
* @param array $input
* @return mixed
*/
public function updateWithIdAndInput($id, array $input)
{
return $this->keyMessageModel->where('id', $id)->update($input);
}


/**
* @param $stageId
* @return mixed
*/
public function findItemsByStageId($stageId)
{
return $this->keyMessageModel->where('stage_id', $stageId)->get();
}
}
13 changes: 13 additions & 0 deletions app/Classes/Repositories/KeyMessageRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Classes\Repositories;

interface KeyMessageRepositoryInterface extends RepositoryInterface
{

/**
* @param $stageId
* @return mixed
*/
public function findItemsByStageId($stageId);
}
83 changes: 83 additions & 0 deletions app/Classes/Repositories/SupportingMessageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Classes\Repositories;

use App\Models\SupportingMessage;

class SupportingMessageRepository implements SupportingMessageRepositoryInterface
{

/**
* @var SupportingMessage
*/
protected $suppotingMessageModel;

/**
* @param SupportingMessage $model
*/
public function __construct(SupportingMessage $model)
{
$this->suppotingMessageModel = $model;
}

/**
* @param array $attributes
* @return static
*/
public function newInstance(array $attributes = [])
{
return $this->suppotingMessageModel->newInstance($attributes);
}

/**
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public function all($columns = ['*'])
{
return $this->suppotingMessageModel->all($columns);
}

/**
* @param array $attributes
* @return static
*/
public function create(array $attributes)
{
return $this->suppotingMessageModel->create($attributes);
}

/**
* @param $id
* @param array $columns
* @return mixed
*/
public function find($id, $columns = ['*'])
{
return $this->suppotingMessageModel->findOrFail($id, $columns);
}

/**
* @param $id
* @return int
*/
public function destroy($id)
{
return $this->suppotingMessageModel->destroy($id);
}

/**
* @param $id
* @param array $input
* @return mixed
*/
public function updateWithIdAndInput($id, array $input)
{
return $this->suppotingMessageModel->where('id', $id)->update($input);
}

public function findItemsByKeyMessageId($keyMessageId)
{
return $this->suppotingMessageModel->where('key_message_id', $keyMessageId)->get();
}
}
13 changes: 13 additions & 0 deletions app/Classes/Repositories/SupportingMessageRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Classes\Repositories;

interface SupportingMessageRepositoryInterface extends RepositoryInterface
{

/**
* @param $keyMessageId
* @return mixed
*/
public function findItemsByKeyMessageId($keyMessageId);
}
9 changes: 6 additions & 3 deletions app/Classes/Repositories/WhatNowRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
class WhatNowRepository implements WhatNowRepositoryInterface
{
public const EVENT_STAGES = [
'mitigation',
'seasonalForecast',
'watch',
'warning',
'immediate',
'recover',
'anticipated',
'assess_and_plan',
'mitigate_risks',
'prepare_to_respond',


];

/**
Expand Down
26 changes: 23 additions & 3 deletions app/Classes/Repositories/WhatNowTranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ class WhatNowTranslationRepository implements WhatNowTranslationRepositoryInterf
*/
protected $whatNowTranslationModel;

/** @var KeyMessageRepositoryInterface */
protected $keyMessageRepository;

/** @var SupportingMessageRepositoryInterface */
protected $supportingMessageRepository;

/**
* @param WhatNowEntityTranslation $whatNowTranslationModel
*/
public function __construct(WhatNowEntityTranslation $whatNowTranslationModel)
public function __construct(WhatNowEntityTranslation $whatNowTranslationModel, KeyMessageRepositoryInterface $keyMessageRepository, SupportingMessageRepositoryInterface $supportingMessageRepository)
{
$this->whatNowTranslationModel = $whatNowTranslationModel;
$this->keyMessageRepository = $keyMessageRepository;
$this->supportingMessageRepository = $supportingMessageRepository;
}

/**
Expand Down Expand Up @@ -73,11 +81,23 @@ public function addTranslations(WhatNowEntity $entity, array $translations)
if (empty($transStage)) {
if (! empty($content)) {
try {
$translation->stages()->create([
$stage = $translation->stages()->create([
'language_code' => $trans['lang'],
'stage' => $stage,
'content' => json_encode($content),
]);
foreach ($content as $message) {
$keyMessage = $this->keyMessageRepository->create([
'entities_stage_id' => $stage->id,
'title' => $message['title'],
]);

foreach ($message['content'] as $supportMessage) {
$this->supportingMessageRepository->create([
'key_message_id' => $keyMessage->id,
'content' => $supportMessage,
]);
}
}
} catch (\Exception $e) {
Log::error('ERROR', [$e->getMessage()]);
}
Expand Down
23 changes: 19 additions & 4 deletions app/Classes/Transformers/WhatNowEntityTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class WhatNowEntityTransformer extends TransformerAbstract
* @param WhatNowTranslationRepositoryInterface $repo
* @param array $configuration
*/

function __construct(WhatNowTranslationRepositoryInterface $repo, $configuration = [])
{
if(isset($configuration['unpublished']) && is_bool($configuration['unpublished'])) {
Expand Down Expand Up @@ -70,7 +71,7 @@ public function transform(WhatNowEntity $model)
'url' => $model->organisation->attribution_url,
'imageUrl' => $model->organisation->attribution_file_name ? $model->organisation->getAttributionImageUrl() : null,
'translations' => null
]
],
];

if ($model->organisation->details->count()) {
Expand All @@ -88,7 +89,7 @@ public function transform(WhatNowEntity $model)
}

if ($this->unpublished) {
$translations = $this->wnTransRepo->getLatestTranslations($model->id);
$translations = $this->wnTransRepo->getLatestTranslations($model->id) ?? [];
} else {
$translations = $this->wnTransRepo->getLatestPublishedTranslations($model->id, $this->lang);
}
Expand All @@ -99,14 +100,28 @@ public function transform(WhatNowEntity $model)
}

if ($translations) {

$response['translations'] = [];
/** @var WhatNowEntityTranslation $trans */
foreach ($translations as $trans) {

$stages = $defaultStages;
if($trans->stages){
foreach ($trans->stages as $stage) {
$stages[$stage->stage] = json_decode($stage['content']);
$stage->load('keyMessages');
$keyMessages = $stage->keyMessages;

foreach ($keyMessages as $keyMessage) {
$keyMessage->load('supportingMessages');
}

$stages[$stage->stage] = $keyMessages->map(function($keyMessage) {
return [
'title' => $keyMessage->title,
'content' => $keyMessage->supportingMessages->map(function($supportingMessage) {
return $supportingMessage->content;
})->toArray()
];
})->toArray();
}
}

Expand Down
8 changes: 8 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@ public function register()
'App\Classes\Repositories\WhatNowTranslationRepositoryInterface',
'App\Classes\Repositories\WhatNowTranslationRepository'
);
$this->app->bind(
'App\Classes\Repositories\KeyMessageRepositoryInterface',
'App\Classes\Repositories\KeyMessageRepository'
);
$this->app->bind(
'App\Classes\Repositories\SupportingMessageRepositoryInterface',
'App\Classes\Repositories\SupportingMessageRepository'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateEnumStageInWhatnowEntityStagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('whatnow_entity_stages', function (Blueprint $table) {
DB::statement("ALTER TABLE whatnow_entity_stages MODIFY COLUMN stage ENUM('immediate', 'warning', 'anticipated', 'assess_and_plan', 'mitigate_risks', 'prepare_to_respond', 'recover') NOT NULL");
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('whatnow_entity_stages', function (Blueprint $table) {
DB::statement("ALTER TABLE whatnow_entity_stages MODIFY COLUMN stage ENUM('immediate', 'warning', 'anticipated', 'assess_and_plan', 'mitigate_risks', 'prepare_to_respond', 'recover') NOT NULL");
});
}
}
3 changes: 1 addition & 2 deletions database/seeds/WhatNowTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public function run()
'id' => $row[0],
'translation_id' => $row[1],
'language_code' => $row[2],
'stage' => $row[3],
'content' => json_encode(json_decode($row[4])),
'stage' => $row[3]
]);

}
Expand Down
Loading