Skip to content

Commit

Permalink
More typing in the project
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil42Russia authored and pronskiy committed Feb 6, 2024
1 parent 1f9ec7e commit 9cb5839
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 75 deletions.
37 changes: 19 additions & 18 deletions app/src/Bundles/AtomFeedGeneratorBundle/AtomFeedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

class AtomFeedGenerator implements EventSubscriberInterface
{
protected Configuration $configuration;
private Configuration $configuration;

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}

public static function getSubscribedEvents(): array
{
Expand All @@ -23,11 +28,6 @@ public static function getSubscribedEvents(): array
];
}

public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}

public function afterRun(SourceSetEvent $sourceSetEvent): void
{
$sourceSet = $sourceSetEvent->sourceSet();
Expand Down Expand Up @@ -75,22 +75,23 @@ public function afterRun(SourceSetEvent $sourceSetEvent): void
$this->generateFeed($entries);
}

/**
* @param Author[] $entries
*/
protected function fetchEntry(Configuration $data, array $authors): Entry
{
$baseUrl = $this->configuration->get('url') ?? 'http://localhost';

$post = new Entry();

$post->title = $data->get('title');
$post->link = $baseUrl . $data->get('url');
$post->authors = $authors;
$post->description = $data->get('blocks.content');
$post->published_at = new \DateTimeImmutable($data->get('published_at'));

return $post;
return new Entry(
$data->get('title'),
$baseUrl . $data->get('url'),
$authors,
$data->get('blocks.content'),
new \DateTimeImmutable($data->get('published_at')),
);
}

protected function slug($author): string
protected function slug(string $author): string
{
return mb_strtolower(preg_replace('/\W/', '_', $author));
}
Expand All @@ -100,10 +101,10 @@ protected function generateFeed(array $entries = []): void
foreach ($entries as $filePath => $posts) {
$rss = new SitemapGenerator();
if ($title = $this->configuration->get('title')) {
$rss->title = $title;
$rss->setTitle($title);
}
if ($subtitle = $this->configuration->get('subtitle')) {
$rss->description = $subtitle;
$rss->setDescription($subtitle);
}

foreach ($posts as $post) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
return new TreeBuilder('sculpin_atom_feed_generator');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class SculpinAtomFeedGeneratorExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
return new TreeBuilder('sculpin_sharing_image_generator');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class SculpinSharingImageGeneratorExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

class SharingImageGenerator implements EventSubscriberInterface
{
protected $configuration;
private Configuration $configuration;

public static function getSubscribedEvents()
public function __construct(Configuration $configuration)
{
return [
Sculpin::EVENT_BEFORE_RUN => 'beforeRun',
];
$this->configuration = $configuration;
}

public function __construct(Configuration $configuration)
public static function getSubscribedEvents(): array
{
$this->configuration = $configuration;
return [
Sculpin::EVENT_BEFORE_RUN => 'beforeRun',
];
}

public function beforeRun(SourceSetEvent $sourceSetEvent)
public function beforeRun(SourceSetEvent $sourceSetEvent): void
{
$sourceSet = $sourceSetEvent->sourceSet();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@

namespace App\Bundles\SharingImageGeneratorBundle;

use Dflydev\DotAccessConfiguration\Configuration;
use Symfony\Component\Filesystem\Filesystem;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class TwigSharingImageGeneratorExtension extends AbstractExtension
{
protected $configuration;
private Configuration $configuration;

public function __construct($configuration)
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}

public function getName()
public function getName(): string
{
return 'sharing_image_generator';
}

public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('sharing_image', [$this, 'getSharingImage']),
];
}

public function getSharingImage($filename)
public function getSharingImage(string $filename): string
{
$filesystem = new Filesystem();

Expand Down
30 changes: 15 additions & 15 deletions app/src/Seo/SharingImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@

use GdImage;

class SharingImageGenerator
final class SharingImageGenerator
{
public const int IMAGE_WIDTH = 1600;
private const int IMAGE_WIDTH = 1600;

public const int IMAGE_HEIGHT = 900;
private const int IMAGE_HEIGHT = 900;

public const int IMAGE_MARGINS = 72;
private const int IMAGE_MARGINS = 72;

public const int TITLE_FONT_SIZE = 72;
private const int TITLE_FONT_SIZE = 72;

public const int AUTHOR_FONT_SIZE = 24;
private const int AUTHOR_FONT_SIZE = 24;

protected bool $inverse = false;
private bool $inverse = false;

protected string $title;
private string $title;

protected ?string $author = null;
private ?string $author = null;

public function setTitle($title): SharingImageGenerator
public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function setAuthor($author): SharingImageGenerator
public function setAuthor(?string $author): self
{
$this->author = $author;

return $this;
}

public function setInverse($inverse): SharingImageGenerator
public function setInverse(bool $inverse): self
{
$this->inverse = $inverse;

Expand All @@ -52,15 +52,15 @@ public function output(): void
imagedestroy($image);
}

public function save($path): void
public function save(string $path): void
{
$image = $this->prepare();

imagepng($image, $path);
imagedestroy($image);
}

protected function prepare(): GdImage
private function prepare(): GdImage
{
$image = imagecreate(self::IMAGE_WIDTH, self::IMAGE_HEIGHT);

Expand Down Expand Up @@ -92,7 +92,7 @@ protected function prepare(): GdImage
$title
);

if ($this->author) {
if ($this->author !== null) {
$author = wordwrap($this->author, 40);

imagettftext(
Expand Down
13 changes: 5 additions & 8 deletions app/src/Sitemap/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace App\Sitemap;

class Author
readonly class Author
{
public function __construct($name, $url)
public function __construct(
public string $name,
public string $url
)
{
$this->name = $name;
$this->url = $url;
}

public string $name;

public string $url;
}
24 changes: 13 additions & 11 deletions app/src/Sitemap/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace App\Sitemap;

class Entry
readonly class Entry
{
public string $title;

public string $link;

/** @var Author[] */
public array $authors = [];

public string $description;

public \DateTimeImmutable $published_at;
/**
* @param Author[] $authors
*/
public function __construct(
public string $title,
public string $link,
public array $authors,
public string $description,
public \DateTimeImmutable $published_at,
)
{
}
}
26 changes: 20 additions & 6 deletions app/src/Sitemap/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@

use DOMDocument;

class SitemapGenerator
final class SitemapGenerator
{
public string $title;
private string $title;

public string $description;
private string $description;

protected array $entries = [];
/** @var Entry[] */
private array $entries = [];

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}

public function setDescription(string $description): self
{
$this->description = $description;

return $this;
}

public function addEntry(Entry $entry): void
{
$this->entries[] = $entry;
}

public function saveFeed($path): void
public function saveFeed(string $path): void
{
$dom = new DOMDocument('1.0', 'utf-8');
$feed = $dom->createElement('feed');
Expand All @@ -32,7 +47,6 @@ public function saveFeed($path): void

$feed->append($dom->createElement('updated', date('c')));

/** @var Entry $entry */
foreach ($this->entries as $entry) {
$element = $dom->createElement('entry');

Expand Down

0 comments on commit 9cb5839

Please sign in to comment.