mirror of
https://github.com/friendica/friendica
synced 2025-04-28 01:10:12 +00:00
Add new paradigm classes for notify
- Create BaseDepository class - Create Entity, Collection, Factory and Depository classes - Create FormattedNotification Entity, Collection and Factory to remove business logic from Notify repository - Create new NotificationCreationIntercepted exception to allow addons to cancel notification creation - Remove unused frio notifications/notify.tpl template
This commit is contained in:
parent
810699b454
commit
1b4e3564a5
11 changed files with 983 additions and 3 deletions
101
src/BaseDepository.php
Normal file
101
src/BaseDepository.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Capabilities\ICanCreateFromTableRow;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Depositories are meant to store and retrieve Entities from the database.
|
||||
*
|
||||
* The reason why there are methods prefixed with an underscore is because PHP doesn't support generic polymorphism
|
||||
* which means we can't direcly overload base methods and make parameters more strict (from a parent class to a child
|
||||
* class for example)
|
||||
*
|
||||
* Similarly, we can't make an overloaded method return type more strict until we only support PHP version 7.4 but this
|
||||
* is less pressing.
|
||||
*/
|
||||
abstract class BaseDepository
|
||||
{
|
||||
const LIMIT = 30;
|
||||
|
||||
/**
|
||||
* @var string This should be set to the main database table name the depository is using
|
||||
*/
|
||||
protected static $table_name;
|
||||
|
||||
/** @var Database */
|
||||
protected $db;
|
||||
|
||||
/** @var LoggerInterface */
|
||||
protected $logger;
|
||||
|
||||
/** @var ICanCreateFromTableRow */
|
||||
protected $factory;
|
||||
|
||||
public function __construct(Database $database, LoggerInterface $logger, ICanCreateFromTableRow $factory)
|
||||
{
|
||||
$this->db = $database;
|
||||
$this->logger = $logger;
|
||||
$this->factory = $factory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
* @return BaseCollection
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _select(array $condition, array $params = []): BaseCollection
|
||||
{
|
||||
$rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
|
||||
|
||||
$Entities = new BaseCollection();
|
||||
foreach ($rows as $fields) {
|
||||
$Entities[] = $this->factory->createFromTableRow($fields);
|
||||
}
|
||||
|
||||
return $Entities;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
* @return BaseEntity
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
protected function _selectOne(array $condition, array $params = []): BaseEntity
|
||||
{
|
||||
$fields = $this->db->selectFirst(static::$table_name, [], $condition, $params);
|
||||
if (!$this->db->isResult($fields)) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return $this->factory->createFromTableRow($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
* @return int
|
||||
* @throws Exception
|
||||
*/
|
||||
public function count(array $condition, array $params = []): int
|
||||
{
|
||||
return $this->db->count(static::$table_name, $condition, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exists(array $condition): bool
|
||||
{
|
||||
return $this->db->exists(static::$table_name, $condition);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue