Move FSuggest to depository

This commit is contained in:
Philipp 2021-10-21 22:57:13 +02:00 committed by Hypolite Petovan
parent 4a50a83437
commit 82a6c78033
13 changed files with 236 additions and 464 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace Friendica\Contact\FriendSuggest\Depository;
use Friendica\BaseDepository;
use Friendica\Contact\FriendSuggest\Exception\FriendSuggestPersistenceException;
use Friendica\Contact\FriendSuggest\Factory;
use Friendica\Contact\FriendSuggest\Entity;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\NotFoundException;
use Psr\Log\LoggerInterface;
class FriendSuggest extends BaseDepository
{
/** @var Factory\FriendSuggest */
protected $factory;
protected static $table_name = 'fsuggest';
public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
{
parent::__construct($database, $logger, $factory);
}
private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
{
return [
'uid' => $fsuggest->uid,
'cid' => $fsuggest->cid,
'name' => $fsuggest->name,
'url' => $fsuggest->url,
'request' => $fsuggest->request,
'photo' => $fsuggest->photo,
'note' => $fsuggest->note,
];
}
/**
* @param array $condition
* @param array $params
*
* @return Entity\FriendSuggest
*
* @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
*/
private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
{
return parent::_selectOne($condition, $params);
}
public function selectOneById(int $id): Entity\FriendSuggest
{
return $this->selectOne(['id' => $id]);
}
public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
{
try {
$fields = $this->convertToTableRow($fsuggest);
if ($fsuggest->id) {
$this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
return $this->factory->createFromTableRow($fields);
} else {
$this->db->insert(self::$table_name, $fields);
return $this->selectOneById($this->db->lastInsertId());
}
} catch (\Exception $exception) {
throw new FriendSuggestPersistenceException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
}
}
}

View file

@ -0,0 +1,83 @@
<?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\FriendSuggest\Entity;
use Friendica\BaseEntity;
/**
* Model for interacting with a friend suggestion
*
* @property-read int $uid
* @property-read int $cid
* @property-read string $name
* @property-read string $url
* @property-read string $request
* @property-read string $photo
* @property-read string $note
* @property-read \DateTime created
* @property-read int|null $id
*/
class FriendSuggest extends BaseEntity
{
/** @var int */
protected $uid;
/** @var int */
protected $cid;
/** @var string */
protected $name;
/** @var string */
protected $url;
/** @var string */
protected $request;
/** @var string */
protected $photo;
/** @var string */
protected $note;
/** @var \DateTime */
protected $created;
/** @var int|null */
protected $id;
/**
* @param int $uid
* @param int $cid
* @param string $name
* @param string $url
* @param string $request
* @param string $photo
* @param string $note
* @param \DateTime $created
* @param int|null $id
*/
public function __construct(int $uid, int $cid, string $name, string $url, string $request, string $photo, string $note, \DateTime $created, ?int $id = null)
{
$this->uid = $uid;
$this->cid = $cid;
$this->name = $name;
$this->url = $url;
$this->request = $request;
$this->photo = $photo;
$this->note = $note;
$this->created = $created;
$this->id = $id;
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace Friendica\Contact\FriendSuggest\Exception;
class FriendSuggestPersistenceException extends \RuntimeException
{
public function __construct($message = "", \Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Friendica\Contact\FriendSuggest\Factory;
use Friendica\BaseFactory;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Contact\FriendSuggest\Entity;
class FriendSuggest extends BaseFactory implements ICanCreateFromTableRow
{
/**
* @inheritDoc
*/
public function createFromTableRow(array $row): Entity\FriendSuggest
{
return new Entity\FriendSuggest(
$row['uid'] ?? 0,
$row['cid'] ?? 0,
$row['name'] ?? '',
$row['url'] ?? '',
$row['request'] ?? '',
$row['photo'] ?? '',
$row['note'] ?? '',
new \DateTime($row['created'] ?? 'now', new \DateTimeZone('UTC')),
$row['id'] ?? null
);
}
public function createNew(
int $uid,
int $cid,
string $name = '',
string $url = '',
string $request = '',
string $photo = '',
string $note = ''
): Entity\FriendSuggest
{
return $this->createFromTableRow([
'uid' => $uid,
'cid' => $cid,
'name' => $name,
'url' => $url,
'request' => $request,
'photo' => $photo,
'note' => $note,
]);
}
}