44namespace MageOS \AsyncEventsAdminUi \Command \AsyncEvent ;
55
66use Exception ;
7+ use Magento \Framework \Encryption \EncryptorInterface ;
78use MageOS \AsyncEvents \Api \Data \AsyncEventInterface ;
8- use MageOS \AsyncEvents \Model \AsyncEvent as AsyncEventModel ;
9- use MageOS \AsyncEvents \Model \AsyncEventFactory as AsyncEventModelFactory ;
109use MageOS \AsyncEvents \Model \ResourceModel \AsyncEvent as AsyncEventResource ;
1110use Magento \Framework \Exception \CouldNotSaveException ;
1211use Psr \Log \LoggerInterface ;
1716class SaveCommand
1817{
1918 private LoggerInterface $ logger ;
20- private AsyncEventModelFactory $ modelFactory ;
2119 private AsyncEventResource $ resource ;
2220
2321 public function __construct (
2422 LoggerInterface $ logger ,
25- AsyncEventModelFactory $ modelFactory ,
26- AsyncEventResource $ resource
23+ AsyncEventResource $ resource ,
24+ private readonly EncryptorInterface $ encryptor
2725 ) {
2826 $ this ->logger = $ logger ;
29- $ this ->modelFactory = $ modelFactory ;
3027 $ this ->resource = $ resource ;
3128 }
3229
@@ -38,15 +35,15 @@ public function __construct(
3835 public function execute (AsyncEventInterface $ asyncEvent ): int
3936 {
4037 try {
41- /** @var AsyncEventModel $model */
42- $ model = $ this ->modelFactory ->create ();
43- $ model ->addData ($ asyncEvent ->getData ());
44- $ model ->setHasDataChanges (true );
38+ $ asyncEvent ->setHasDataChanges (true );
4539
46- if (!$ model ->getSubscriptionId ()) {
47- $ model ->isObjectNew (true );
40+ if (!$ asyncEvent ->getSubscriptionId ()) {
41+ $ asyncEvent ->isObjectNew (true );
42+ $ asyncEvent ->setSubscribedAt ((new \DateTime ())->format (\DateTimeInterface::ATOM ));
43+ $ secretVerificationToken = $ this ->encryptor ->encrypt ($ asyncEvent ->getVerificationToken ());
44+ $ asyncEvent ->setVerificationToken ($ secretVerificationToken );
4845 }
49- $ this ->resource ->save ($ model );
46+ $ this ->resource ->save ($ asyncEvent );
5047 } catch (Exception $ exception ) {
5148 $ this ->logger ->error (
5249 __ ('Could not save Asynchronous Event Subscriber. Original message: {message} ' ),
@@ -58,6 +55,6 @@ public function execute(AsyncEventInterface $asyncEvent): int
5855 throw new CouldNotSaveException (__ ('Could not save Asynchronous Event Subscriber. ' ));
5956 }
6057
61- return (int )$ model ->getSubscriptionId ();
58+ return (int )$ asyncEvent ->getSubscriptionId ();
6259 }
6360}
0 commit comments