Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions db/migrations/20251218230144_update_planete_flux_nullables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class UpdatePlaneteFluxNullables extends AbstractMigration
{
public function change(): void
{
($table = $this->table('afup_planete_flux'))
->changeColumn('nom', $table->getColumn('nom')->getType(), [
'null' => false,
])
->changeColumn('url', $table->getColumn('url')->getType(), [
'null' => false,
])
->changeColumn('feed', $table->getColumn('feed')->getType(), [
'null' => false,
])
->changeColumn('etat', $table->getColumn('etat')->getType(), [
'null' => false,
])
->update();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(

public function __invoke(Request $request): RedirectResponse
{
$id = $request->query->get('id');
$id = $request->query->getInt('id');
if ($this->feedRepository->delete($id)) {
$this->audit->log('Suppression du flux ' . $id);
$this->addFlash('notice', 'Le flux a été supprimé');
Expand Down
10 changes: 5 additions & 5 deletions sources/AppBundle/Controller/Admin/Planete/FeedEditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function __invoke(Request $request): Response
$id = $request->query->getInt('id');
$feed = $this->feedRepository->get($id);
$data = new FeedFormData();
$data->name = $feed->getName();
$data->feed = $feed->getFeed();
$data->url = $feed->getUrl();
$data->userId = $feed->getUserId();
$data->status = $feed->getStatus();
$data->name = $feed->name;
$data->feed = $feed->feed;
$data->url = $feed->url;
$data->userId = $feed->userId;
$data->status = $feed->status;
$form = $this->createForm(FeedFormType::class, $data);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
Expand Down
5 changes: 3 additions & 2 deletions sources/AppBundle/Controller/Admin/Planete/FeedListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PlanetePHP\Feed;
use PlanetePHP\FeedRepository;
use PlanetePHP\FeedStatus;
use PlanetePHP\FeedTester;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -47,8 +48,8 @@ private function testFeeds(array $feeds): array
$results = [];

foreach ($feeds as $feed) {
if ($feed->getStatus()) {
$results[$feed->getId()] = $this->feedTester->test($feed);
if ($feed->status === FeedStatus::Active) {
$results[$feed->id] = $this->feedTester->test($feed);
}
}

Expand Down
4 changes: 2 additions & 2 deletions sources/AppBundle/Controller/Planete/FeedsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __invoke(): Response

foreach ($feeds as $feed) {
$data[] = [
'name' => $feed->getName(),
'url' => $feed->getUrl(),
'name' => $feed->name,
'url' => $feed->url,
];
}

Expand Down
33 changes: 11 additions & 22 deletions sources/AppBundle/Planete/FeedFormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,24 @@

namespace AppBundle\Planete;

use PlanetePHP\Feed;
use PlanetePHP\FeedStatus;
use Symfony\Component\Validator\Constraints as Assert;

class FeedFormData
{
/**
* @var string
*/
#[Assert\NotBlank]
public $name;
/**
* @var string
*/
public string $name;

#[Assert\NotBlank]
#[Assert\Url]
public $url = 'https://';
/**
* @var string
*/
public string $url = 'https://';

#[Assert\NotBlank]
#[Assert\Url]
public $feed = 'https://';
/**
* @var int|null
*/
public $userId;
/**
* @var int
*/
#[Assert\Choice(choices: [Feed::STATUS_INACTIVE, Feed::STATUS_ACTIVE], strict: true)]
public $status = Feed::STATUS_ACTIVE;
public string $feed = 'https://';

public ?int $userId = null;

#[Assert\Choice(choices: [FeedStatus::Inactive, FeedStatus::Active], strict: true)]
public FeedStatus $status = FeedStatus::Active;
}
14 changes: 6 additions & 8 deletions sources/AppBundle/Planete/FeedFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
namespace AppBundle\Planete;

use AppBundle\Association\Model\Repository\UserRepository;
use PlanetePHP\Feed;
use PlanetePHP\FeedStatus;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
Expand All @@ -19,7 +20,7 @@ public function __construct(private readonly UserRepository $userRepository) {}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$users = [null => ''];
$users = [null => null];
foreach ($this->userRepository->search() as $user) {
$users[$user->getLastName() . ' ' . $user->getFirstName()] = $user->getId();
}
Expand Down Expand Up @@ -53,13 +54,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'choices' => $users,
])
->add('status', ChoiceType::class, [
'label' => 'Etat',
->add('status', EnumType::class, [
'label' => 'État',
'required' => true,
'choices' => [
'Actif' => Feed::STATUS_ACTIVE,
'Inactif' => Feed::STATUS_INACTIVE,
],
'class' => FeedStatus::class,
])
->add('save', SubmitType::class, ['label' => 'Ajouter']);
}
Expand Down
55 changes: 7 additions & 48 deletions sources/PlanetePHP/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,14 @@

namespace PlanetePHP;

class Feed
final readonly class Feed
{
public const STATUS_ACTIVE = 1;
public const STATUS_INACTIVE = 0;

/**
* @param int $id
* @param string $name
* @param string $url
* @param string $feed
* @param int $status
* @param int $userId
*/
public function __construct(
private $id,
private $name,
private $url,
private $feed,
private $status,
private $userId,
public int $id,
public string $name,
public string $url,
public string $feed,
public FeedStatus $status,
public ?int $userId,
) {}

public function getId()
{
return $this->id;
}

public function getName()
{
return $this->name;
}

public function getUrl()
{
return $this->url;
}

public function getFeed()
{
return $this->feed;
}

public function getStatus()
{
return $this->status;
}

public function getUserId()
{
return $this->userId;
}
}
20 changes: 10 additions & 10 deletions sources/PlanetePHP/FeedCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Laminas\Feed\Reader\Reader;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;

final readonly class FeedCrawler
{
Expand All @@ -32,22 +32,22 @@ public function crawl(): CrawlingResult
$failedFeedsIds = [];

foreach ($feeds as $feed) {
$this->logger->info(sprintf('[planete][%s] Start fetching', $feed->getName()));
$this->logger->info(sprintf('[planete][%s] Start fetching', $feed->name));

try {
$items = Reader::import($feed->getFeed());
} catch (ExceptionInterface|ClientException|ServerException $e) {
$this->logger->error(sprintf('[planete][%s] Error: %s', $feed->getName(), $e->getMessage()));
$items = Reader::import($feed->feed);
} catch (ExceptionInterface|ClientExceptionInterface|ServerExceptionInterface $e) {
$this->logger->error(sprintf('[planete][%s] Error: %s', $feed->name, $e->getMessage()));

// Si une erreur survient, on passe au flux suivant
$failedFeedsIds[] = $feed->getId();
$failedFeedsIds[] = $feed->id;
continue;
}

$this->logger->info(sprintf('[planete][%s] Items: %d', $feed->getName(), count($items)));
$this->logger->info(sprintf('[planete][%s] Items: %d', $feed->name, count($items)));

foreach ($items as $item) {
$this->logger->info(sprintf('[planete][%s] Item: %s', $feed->getName(), $item->getTitle()));
$this->logger->info(sprintf('[planete][%s] Item: %s', $feed->name, $item->getTitle()));

$date = $item->getDateCreated();

Expand All @@ -64,7 +64,7 @@ public function crawl(): CrawlingResult

$article = new FeedArticle(
null,
$feed->getId(),
$feed->id,
$item->getId(),
$item->getTitle(),
$item->getLink(),
Expand Down
29 changes: 8 additions & 21 deletions sources/PlanetePHP/FeedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function findActive(): array
{
$query = $this->connection->prepare('SELECT id, nom, url, feed, etat, id_personne_physique
FROM afup_planete_flux f WHERE f.etat = :status ORDER BY f.nom');
$query->bindValue('status', Feed::STATUS_ACTIVE);
$query->bindValue('status', FeedStatus::Active->value);

return $this->hydrateAll($query->executeQuery()->fetchAllAssociative());
}
Expand Down Expand Up @@ -59,20 +59,20 @@ public function get($id): Feed
return $this->hydrate($query->executeQuery()->fetchAssociative());
}

public function insert($name, $url, $feed, $status, $userId = 0)
public function insert(string $name, string $url, string $feed, FeedStatus $status, ?int $userId = 0)
{
$statement = $this->connection->prepare('INSERT INTO afup_planete_flux (nom, url, feed, etat, id_personne_physique) VALUES (:name, :url, :feed, :status, :userId)');

$statement->bindValue('name', $name);
$statement->bindValue('url', $url);
$statement->bindValue('feed', $feed);
$statement->bindValue('status', $status);
$statement->bindValue('status', $status->value);
$statement->bindValue('userId', (int) $userId);

return $statement->executeStatement();
}

public function update($id, $name, $url, $feed, $status, $userId = 0)
public function update(int $id, string $name, string $url, string $feed, FeedStatus $status, ?int $userId = 0)
{
$statement = $this->connection->prepare('UPDATE afup_planete_flux
SET nom = :name, url = :url, feed = :feed, etat = :status, id_personne_physique = :userId
Expand All @@ -81,29 +81,16 @@ public function update($id, $name, $url, $feed, $status, $userId = 0)
$statement->bindValue('name', $name);
$statement->bindValue('url', $url);
$statement->bindValue('feed', $feed);
$statement->bindValue('status', $status);
$statement->bindValue('status', $status->value);
$statement->bindValue('userId', (int) $userId);
$statement->bindValue('id', $id);

return $statement->executeStatement();
}

public function delete($id)
public function delete(int $id): bool
{
return $this->connection->delete('afup_planete_flux', ['id' => $id]);
}

public function getListByLatest()
{
return $this->connection->executeQuery(<<<SQL
SELECT MAX(b.id) id, f.nom, f.url, MAX(b.maj) updatedAt
FROM afup_planete_billet b
INNER JOIN afup_planete_flux f ON b.afup_planete_flux_id = f.id
WHERE b.etat = 1 AND f.etat = 1
GROUP BY f.id
ORDER BY updatedAt DESC
SQL
)->fetchAllAssociative();
return $this->connection->delete('afup_planete_flux', ['id' => $id]) === 1;
}

private function hydrateAll(array $rows): array
Expand All @@ -118,7 +105,7 @@ private function hydrate(array $row): Feed
$row['nom'],
$row['url'],
$row['feed'],
$row['etat'],
FeedStatus::from($row['etat']),
$row['id_personne_physique'],
);
}
Expand Down
22 changes: 22 additions & 0 deletions sources/PlanetePHP/FeedStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PlanetePHP;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum FeedStatus: int implements TranslatableInterface
{
case Inactive = 0;
case Active = 1;

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return match ($this) {
self::Inactive => 'Inactif',
self::Active => 'Actif',
};
}
}
2 changes: 1 addition & 1 deletion sources/PlanetePHP/FeedTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(
public function test(Feed $feed): bool
{
try {
$xml = $this->feedClient->get($feed->getFeed());
$xml = $this->feedClient->get($feed->feed);

new SimpleXmlElement($xml->getBody());

Expand Down
Loading