mirror of
https://github.com/friendica/friendica
synced 2025-09-02 20:45:03 +02:00
Move Introduction to new depository paradigm
This commit is contained in:
parent
4c0e00fa4f
commit
a40f503fdd
14 changed files with 380 additions and 179 deletions
9
src/Contact/Introduction/Collection/Introductions.php
Normal file
9
src/Contact/Introduction/Collection/Introductions.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Contact\Introduction\Collection;
|
||||
|
||||
use Friendica\BaseCollection;
|
||||
|
||||
class Introductions extends BaseCollection
|
||||
{
|
||||
}
|
168
src/Contact/Introduction/Depository/Introduction.php
Normal file
168
src/Contact/Introduction/Depository/Introduction.php
Normal file
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Contact\Introduction\Depository;
|
||||
|
||||
use Friendica\BaseDepository;
|
||||
use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
|
||||
use Friendica\Contact\Introduction\Exception\IntroductionPersistenceException;
|
||||
use Friendica\Contact\Introduction\Collection;
|
||||
use Friendica\Contact\Introduction\Entity;
|
||||
use Friendica\Contact\Introduction\Factory;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Network\HTTPException\NotFoundException;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Introduction extends BaseDepository
|
||||
{
|
||||
/** @var Factory\Introduction */
|
||||
protected $factory;
|
||||
|
||||
protected static $table_name = 'intro';
|
||||
|
||||
public function __construct(Database $database, LoggerInterface $logger, Factory\Introduction $factory)
|
||||
{
|
||||
parent::__construct($database, $logger, $factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $params
|
||||
*
|
||||
* @return Entity\Introduction
|
||||
*
|
||||
* @throws NotFoundException the underlying exception if there's no Introduction with the given conditions
|
||||
*/
|
||||
private function selectOne(array $condition, array $params = []): Entity\Introduction
|
||||
{
|
||||
return parent::_selectOne($condition, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a given Introduction into a DB compatible row array
|
||||
*
|
||||
* @param Entity\Introduction $introduction
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function convertToTableRow(Entity\Introduction $introduction): array
|
||||
{
|
||||
return [
|
||||
'uid' => $introduction->uid,
|
||||
'fid' => $introduction->fid,
|
||||
'contact-id' => $introduction->cid,
|
||||
'suggest-cid' => $introduction->sid,
|
||||
'knowyou' => $introduction->knowyou ? 1 : 0,
|
||||
'duplex' => $introduction->duplex ? 1 : 0,
|
||||
'note' => $introduction->note,
|
||||
'hash' => $introduction->hash,
|
||||
'blocked' => $introduction->blocked ? 1 : 0,
|
||||
'ignore' => $introduction->ignore ? 1 : 0
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param int $uid
|
||||
*
|
||||
* @return Entity\Introduction
|
||||
*
|
||||
* @throws IntroductionNotFoundException in case there is no Introduction with this id
|
||||
*/
|
||||
public function selectOneById(int $id, int $uid): Entity\Introduction
|
||||
{
|
||||
try {
|
||||
return $this->selectOne(['id' => $id, 'uid' => $uid]);
|
||||
} catch (NotFoundException $exception) {
|
||||
throw new IntroductionNotFoundException(sprintf('There is no Introduction with the ID %d for the user %d', $id, $uid), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects introductions for a given user
|
||||
*
|
||||
* @param int $uid
|
||||
* @param int|null $min_id
|
||||
* @param int|null $max_id
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection\Introductions
|
||||
*/
|
||||
public function selectForUser(int $uid, int $min_id = null, int $max_id = null, int $limit = self::LIMIT): Collection\Introductions
|
||||
{
|
||||
try {
|
||||
$BaseCollection = parent::_selectByBoundaries(
|
||||
['`uid = ?` AND NOT `ignore`',$uid],
|
||||
['order' => ['id' => 'DESC']],
|
||||
$min_id, $max_id, $limit);
|
||||
} catch (\Exception $e) {
|
||||
throw new IntroductionPersistenceException(sprintf('Cannot select Introductions for used %d', $uid), $e);
|
||||
}
|
||||
|
||||
return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity\Introduction $introduction
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws IntroductionPersistenceException in case the underlying storage cannot delete the Introduction
|
||||
*/
|
||||
public function delete(Entity\Introduction $introduction): bool
|
||||
{
|
||||
if (!$introduction->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->db->delete(self::$table_name, ['id' => $introduction->id]);
|
||||
} catch (\Exception $e) {
|
||||
throw new IntroductionPersistenceException(sprintf('Cannot delete Introduction with id %d', $introduction->id), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity\Introduction $introduction
|
||||
*
|
||||
* @return Entity\Introduction
|
||||
*
|
||||
* @throws IntroductionPersistenceException In case the underlying storage cannot save the Introduction
|
||||
*/
|
||||
public function save(Entity\Introduction $introduction): Entity\Introduction
|
||||
{
|
||||
try {
|
||||
$fields = $this->convertToTableRow($introduction);
|
||||
$fields['datetime'] = DateTimeFormat::utcNow();
|
||||
|
||||
if ($introduction->id) {
|
||||
$this->db->update(self::$table_name, $fields, ['id' => $introduction->id]);
|
||||
return $this->factory->createFromTableRow($fields);
|
||||
} else {
|
||||
$this->db->insert(self::$table_name, $fields);
|
||||
return $this->selectOneById($this->db->lastInsertId(), $introduction->uid);
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new IntroductionPersistenceException(sprintf('Cannot insert/update the Introduction %d for user %d', $introduction->id, $introduction->uid), $exception);
|
||||
}
|
||||
}
|
||||
}
|
98
src/Contact/Introduction/Entity/Introduction.php
Normal file
98
src/Contact/Introduction/Entity/Introduction.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Contact\Introduction\Entity;
|
||||
|
||||
use Friendica\BaseEntity;
|
||||
|
||||
/**
|
||||
* @property-read int $uid
|
||||
* @property-read int $fid
|
||||
* @property-read int $cid
|
||||
* @property-read int $sid
|
||||
* @property-read bool $knowyou
|
||||
* @property-read bool $duplex
|
||||
* @property-read string $note
|
||||
* @property-read string $hash
|
||||
* @property-read \DateTime $datetime
|
||||
* @property-read bool $blocked
|
||||
* @property-read bool $ignore
|
||||
* @property-read int|null $id
|
||||
*/
|
||||
class Introduction extends BaseEntity
|
||||
{
|
||||
/** @var int */
|
||||
protected $uid;
|
||||
/** @var int */
|
||||
protected $fid;
|
||||
/** @var int */
|
||||
protected $cid;
|
||||
/** @var int */
|
||||
protected $sid;
|
||||
/** @var bool */
|
||||
protected $knowyou;
|
||||
/** @var bool */
|
||||
protected $duplex;
|
||||
/** @var string */
|
||||
protected $note;
|
||||
/** @var string */
|
||||
protected $hash;
|
||||
/** @var \DateTime */
|
||||
protected $datetime;
|
||||
/** @var bool */
|
||||
protected $blocked;
|
||||
/** @var bool */
|
||||
protected $ignore;
|
||||
/** @var int|null */
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* @param int $uid
|
||||
* @param int $fid
|
||||
* @param int $cid
|
||||
* @param bool $knowyou
|
||||
* @param bool $duplex
|
||||
* @param string $note
|
||||
* @param string $hash
|
||||
* @param bool $blocked
|
||||
* @param bool $ignore
|
||||
* @param int|null $id
|
||||
*/
|
||||
public function __construct(int $uid, int $fid, int $cid, int $sid, bool $knowyou, bool $duplex, string $note, string $hash, \DateTime $datetime, bool $blocked, bool $ignore, ?int $id)
|
||||
{
|
||||
$this->uid = $uid;
|
||||
$this->fid = $fid;
|
||||
$this->cid = $cid;
|
||||
$this->sid = $sid;
|
||||
$this->knowyou = $knowyou;
|
||||
$this->duplex = $duplex;
|
||||
$this->note = $note;
|
||||
$this->hash = $hash;
|
||||
$this->blocked = $blocked;
|
||||
$this->ignore = $ignore;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function setIgnore()
|
||||
{
|
||||
$this->ignore = true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Contact\Introduction\Exception;
|
||||
|
||||
class IntroductionNotFoundException extends \OutOfBoundsException
|
||||
{
|
||||
public function __construct($message = "", \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 404, $previous);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Contact\Introduction\Exception;
|
||||
|
||||
class IntroductionPersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
50
src/Contact/Introduction/Factory/Introduction.php
Normal file
50
src/Contact/Introduction/Factory/Introduction.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Contact\Introduction\Factory;
|
||||
|
||||
use Friendica\BaseFactory;
|
||||
use Friendica\Contact\Introduction\Entity;
|
||||
use Friendica\Capabilities\ICanCreateFromTableRow;
|
||||
|
||||
class Introduction extends BaseFactory implements ICanCreateFromTableRow
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function createFromTableRow(array $row): Entity\Introduction
|
||||
{
|
||||
return new Entity\Introduction(
|
||||
$row['uid'],
|
||||
$row['fid'],
|
||||
$row['contact-id'],
|
||||
$row['suggested-cid'],
|
||||
!empty($row['knowyou']),
|
||||
!empty($row['dupley']),
|
||||
$row['note'],
|
||||
$row['hash'],
|
||||
new \DateTime($row['datetime'], new \DateTimeZone('UTC')),
|
||||
!empty($row['blocked']),
|
||||
!empty($row['ignore']),
|
||||
$row['id'] ?? null
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue