-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
263 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\components; | ||
|
||
use app\models\backgroundJobs\IBackgroundJob; | ||
|
||
class BackgroundJobScheduler | ||
{ | ||
public static function executeOrScheduleJob(IBackgroundJob $job): void | ||
{ | ||
\Yii::$app->getDb()->createCommand( | ||
'INSERT INTO backgroundJob (`siteId`, `consultationId`, `type`, `dateCreation`, `payload`) VALUES (:siteId, :consultationId, :type, NOW(), :payload)', | ||
[ | ||
':siteId' => $job->getSite()?->id, | ||
':consultationId' => $job->getConsultation()?->id, | ||
':type' => $job->getTypeId(), | ||
':payload' => $job->toJson(), | ||
] | ||
)->execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace app\models\backgroundJobs; | ||
|
||
use app\components\Tools; | ||
use app\models\db\{Consultation, Site}; | ||
use Symfony\Component\Serializer\Annotation\Ignore; | ||
|
||
abstract class IBackgroundJob | ||
{ | ||
protected ?Consultation $consultation = null; | ||
protected ?Site $site = null; | ||
protected ?int $id = null; | ||
|
||
abstract public function getTypeId(): string; | ||
|
||
abstract public function execute(): void; | ||
|
||
/** | ||
* @return array<string, class-string<IBackgroundJob>> | ||
*/ | ||
public static function getAllBackgroundJobs(): array | ||
{ | ||
return [ | ||
SendNotification::TYPE_ID => SendNotification::class, | ||
]; | ||
} | ||
|
||
public function toJson(): string | ||
{ | ||
$serializer = Tools::getSerializer(); | ||
|
||
return $serializer->serialize($this, 'json'); | ||
} | ||
|
||
/** | ||
* @Ignore | ||
*/ | ||
public function getConsultation(): ?Consultation | ||
{ | ||
return $this->consultation; | ||
} | ||
|
||
/** | ||
* @Ignore | ||
*/ | ||
public function getSite(): ?Site | ||
{ | ||
return $this->site; | ||
} | ||
|
||
/** | ||
* @Ignore | ||
*/ | ||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public static function fromJson(int $id, string $typeId, ?int $siteId, ?int $consultationId, string $json): IBackgroundJob | ||
{ | ||
$serializer = Tools::getSerializer(); | ||
|
||
$class = self::getAllBackgroundJobs()[$typeId]; | ||
|
||
/** @var IBackgroundJob $job */ | ||
$job = $serializer->deserialize($json, $class, 'json'); | ||
$job->id = $id; | ||
if ($siteId !== null) { | ||
$job->site = Site::findOne(['id' => $siteId]); | ||
} | ||
if ($consultationId !== null) { | ||
$job->consultation = Consultation::findOne(['id' => $consultationId]); | ||
} | ||
|
||
return $job; | ||
} | ||
} |
Oops, something went wrong.