first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage\Bag;
use Flasher\Prime\Notification\Envelope;
final class ArrayBag implements BagInterface
{
/**
* @var Envelope[]
*/
private $envelopes = array();
/**
* {@inheritDoc}
*/
public function get()
{
return $this->envelopes;
}
/**
* {@inheritDoc}
*/
public function set(array $envelopes)
{
$this->envelopes = $envelopes;
}
}

View File

@ -0,0 +1,25 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage\Bag;
use Flasher\Prime\Notification\Envelope;
interface BagInterface
{
/**
* @return Envelope[]
*/
public function get();
/**
* @param Envelope[] $envelopes
*
* @return void
*/
public function set(array $envelopes);
}

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage\Bag;
use Flasher\Prime\Notification\Envelope;
final class StaticBag implements BagInterface
{
/**
* @var Envelope[]
*/
private static $envelopes = array();
/**
* {@inheritDoc}
*/
public function get()
{
return self::$envelopes;
}
/**
* {@inheritDoc}
*/
public function set(array $envelopes)
{
self::$envelopes = $envelopes;
}
}

View File

@ -0,0 +1,83 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage;
use Flasher\Prime\Stamp\UuidStamp;
use Flasher\Prime\Storage\Bag\ArrayBag;
use Flasher\Prime\Storage\Bag\BagInterface;
final class StorageBag implements StorageInterface
{
/**
* @var BagInterface
*/
private $bag;
public function __construct(BagInterface $bag = null)
{
$this->bag = null !== $bag && 'cli' !== \PHP_SAPI ? $bag : new ArrayBag();
}
/**
* {@inheritdoc}
*/
public function all()
{
return array_values($this->bag->get());
}
/**
* {@inheritdoc}
*/
public function add($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$envelopes = UuidStamp::indexByUuid($envelopes);
$stored = UuidStamp::indexByUuid($this->all());
$envelopes = array_merge($stored, $envelopes);
$this->bag->set(array_values($envelopes));
}
/**
* {@inheritdoc}
*/
public function update($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$envelopes = UuidStamp::indexByUuid($envelopes);
$stored = UuidStamp::indexByUuid($this->all());
$envelopes = array_merge($stored, $envelopes);
$this->bag->set(array_values($envelopes));
}
/**
* {@inheritdoc}
*/
public function remove($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$envelopes = UuidStamp::indexByUuid($envelopes);
$stored = UuidStamp::indexByUuid($this->all());
$envelopes = array_diff_key($stored, $envelopes);
$this->bag->set(array_values($envelopes));
}
/**
* {@inheritdoc}
*/
public function clear()
{
$this->bag->set(array());
}
}

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage;
use Flasher\Prime\Notification\Envelope;
interface StorageInterface
{
/**
* @return Envelope[]
*/
public function all();
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function add($envelopes);
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function update($envelopes);
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function remove($envelopes);
/**
* Remove all notifications from the storage.
*
* @return void
*/
public function clear();
}

View File

@ -0,0 +1,128 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage;
use Flasher\Prime\EventDispatcher\Event\FilterEvent;
use Flasher\Prime\EventDispatcher\Event\PersistEvent;
use Flasher\Prime\EventDispatcher\Event\PostPersistEvent;
use Flasher\Prime\EventDispatcher\Event\PostRemoveEvent;
use Flasher\Prime\EventDispatcher\Event\PostUpdateEvent;
use Flasher\Prime\EventDispatcher\Event\RemoveEvent;
use Flasher\Prime\EventDispatcher\Event\UpdateEvent;
use Flasher\Prime\EventDispatcher\EventDispatcher;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
final class StorageManager implements StorageManagerInterface
{
/**
* @var StorageInterface
*/
private $storage;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var mixed[]
*/
private $criteria = array();
/**
* @param mixed[] $criteria
*/
public function __construct(StorageInterface $storage = null, EventDispatcherInterface $eventDispatcher = null, array $criteria = array())
{
$this->storage = $storage ?: new StorageBag();
$this->eventDispatcher = $eventDispatcher ?: new EventDispatcher();
$this->criteria = $criteria;
}
/**
* {@inheritdoc}
*/
public function all()
{
return $this->storage->all();
}
/**
* {@inheritdoc}
*/
public function filter(array $criteria = array())
{
$criteria = array_merge($this->criteria, $criteria);
$criteria['delay'] = 0;
// @phpstan-ignore-next-line
$criteria['hops']['min'] = 1;
$event = new FilterEvent($this->all(), $criteria);
$this->eventDispatcher->dispatch($event);
return $event->getEnvelopes();
}
/**
* {@inheritdoc}
*/
public function add($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$event = new PersistEvent($envelopes);
$this->eventDispatcher->dispatch($event);
$this->storage->add($event->getEnvelopes());
$event = new PostPersistEvent($event->getEnvelopes());
$this->eventDispatcher->dispatch($event);
}
/**
* {@inheritdoc}
*/
public function update($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$event = new UpdateEvent($envelopes);
$this->eventDispatcher->dispatch($event);
$this->storage->update($event->getEnvelopes());
$event = new PostUpdateEvent($event->getEnvelopes());
$this->eventDispatcher->dispatch($event);
}
/**
* {@inheritdoc}
*/
public function remove($envelopes)
{
$envelopes = \is_array($envelopes) ? $envelopes : \func_get_args();
$event = new RemoveEvent($envelopes);
$this->eventDispatcher->dispatch($event);
$this->storage->update($event->getEnvelopesToKeep());
$this->storage->remove($event->getEnvelopesToRemove());
$event = new PostRemoveEvent($event->getEnvelopesToRemove(), $event->getEnvelopesToKeep());
$this->eventDispatcher->dispatch($event);
}
/**
* {@inheritdoc}
*/
public function clear()
{
$this->storage->clear();
}
}

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the PHPFlasher package.
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
*/
namespace Flasher\Prime\Storage;
use Flasher\Prime\Notification\Envelope;
interface StorageManagerInterface
{
/**
* @return Envelope[]
*/
public function all();
/**
* @param mixed[] $criteria
*
* @return Envelope[]
*/
public function filter(array $criteria = array());
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function add($envelopes);
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function update($envelopes);
/**
* @param Envelope|Envelope[] $envelopes
*
* @return void
*/
public function remove($envelopes);
/**
* remove All notifications from storage.
*
* @return void
*/
public function clear();
}