Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seo static map #391

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TWITTER_CLIENT_SECRET=ThisTokenIsNotSoSecretChangeIt
YOURLS_API_URL=http://sqi.be/yourls-api.php
YOURLS_API_USERNAME=username
YOURLS_API_PASSWORD=password
STATICMAPS_HOST=https://maps.caldera.cc/

ADMIN_PASSWORD=123

Expand Down
9 changes: 8 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
yourls.api_url: '%env(YOURLS_HOSTNAME)%'
yourls.api_username: '%env(YOURLS_USERNAME)%'
yourls.api_password: '%env(YOURLS_PASSWORD)%'
staticmaps.host: '%env(STATICMAPS_HOST)%'
assets_version: '%env(ASSETS_VERSION)%'
router.request_context.host: 'luft.jetzt'
router.request_context.scheme: 'https'
Expand All @@ -27,6 +28,7 @@ services:
$apiPassword: '%env(YOURLS_API_PASSWORD)%'
$assetsVersion: '%assets_version%'
$graphCacheDirectory: '%env(GRAPH_CACHE_DIRECTORY)%'
$staticmapsHost: '%staticmaps.host%'

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand All @@ -43,6 +45,11 @@ services:
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

Sonata\SeoBundle\Seo\SeoPageInterface:
alias: sonata.seo.page.default

App\StationLoader\StationLoader: ~

App\Twitter\MessageFactory\MessageFactoryInterface:
alias: App\Twitter\MessageFactory\ExtendedEmojiMessageFactory

Expand Down Expand Up @@ -83,4 +90,4 @@ services:
$producer: '@old_sound_rabbit_mq.luft_value_producer'

App\Producer\Value\ValueProducerInterface:
alias: App\Producer\Value\CacheValueProducer
alias: App\Producer\Value\CacheValueProducer
26 changes: 26 additions & 0 deletions src/Controller/DisplayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ class DisplayController extends AbstractController
{
public function indexAction(Request $request, SeoPage $seoPage, RequestConverterInterface $requestConverter, PollutionDataFactoryInterface $pollutionDataFactory, CityGuesserInterface $cityGuesser, Breadcrumbs $breadcrumbs, RouterInterface $router): Response
{
/** @var Station $station */
$station = $this->getDoctrine()->getRepository(Station::class)->findOneByStationCode($stationCode);

if (!$station) {
throw $this->createNotFoundException();
}

$boxList = $pollutionDataFactory->setCoord($station)->createDecoratedBoxList();

if ($station->getCity()) {
$seoPage->setTitle(sprintf('Luftmesswerte für die Station %s in %s', $station->getStationCode(), $station->getCity()->getName()));
} else {
$seoPage->setTitle(sprintf('Luftmesswerte für die Station %s', $station->getStationCode()));
}

$seoPage->setPreviewMap($station);

return $this->render('Default/station.html.twig', [
'station' => $station,
'boxList' => $boxList,
]);
}

public function indexAction(Request $request, SeoPage $seoPage, PollutionDataFactory $pollutionDataFactory, StationFinderInterface $stationFinder): Response
{
$coord = $this->getCoordByRequest($request);
$coord = $requestConverter->getCoordByRequest($request);

if (!$coord) {
Expand Down
15 changes: 15 additions & 0 deletions src/DependencyInjection/AppExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}
}
19 changes: 19 additions & 0 deletions src/DependencyInjection/Compiler/TwigSeoExtensionPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace App\DependencyInjection\Compiler;

use App\Twig\Extension\SeoTwigExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class TwigSeoExtensionPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasDefinition('sonata.seo.twig.extension')) {
return;
}

$container->removeDefinition('sonata.seo.twig.extension');
}
}
17 changes: 17 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('caldera');

return $treeBuilder;
}
}
39 changes: 38 additions & 1 deletion src/Entity/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\StaticMap\StaticMapableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -12,7 +13,7 @@
* @ORM\Table(name="city")
* @JMS\ExclusionPolicy("ALL")
*/
class City
class City implements StaticMapableInterface
{
/**
* @ORM\Id
Expand Down Expand Up @@ -46,6 +47,18 @@ class City
*/
protected $description;

/**
* @ORM\Column(type="float", nullable=false)
* @JMS\Expose()
*/
protected $latitude;

/**
* @ORM\Column(type="float", nullable=false)
* @JMS\Expose()
*/
protected $longitude;

/**
* @ORM\Column(type="string", nullable=true)
* @JMS\Expose()
Expand Down Expand Up @@ -93,6 +106,30 @@ public function setCreatedAt(\DateTime $createdAt): City
return $this;
}

public function getLatitude(): float
{
return $this->latitude;
}

public function setLatitude(float $latitude): City
{
$this->latitude = $latitude;

return $this;
}

public function getLongitude(): float
{
return $this->longitude;
}

public function setLongitude(float $longitude): City
{
$this->longitude = $longitude;

return $this;
}

public function getName(): ?string
{
return $this->name;
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/Station.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\StaticMap\StaticMapableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Caldera\GeoBasic\Coord\Coord;
Expand All @@ -17,7 +18,7 @@
* @UniqueEntity("stationCode")
* @JMS\ExclusionPolicy("ALL")
*/
class Station extends Coord
class Station extends Coord implements StaticMapableInterface
{
/**
* @ORM\Id
Expand Down
4 changes: 4 additions & 0 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use App\DependencyInjection\Compiler\TwigSeoExtensionPass;
use App\Air\AirQuality\PollutionLevel\PollutionLevelInterface;
use App\Air\Measurement\MeasurementInterface;
use App\DependencyInjection\Compiler\PollutionLevelCompilerPass;
Expand All @@ -10,6 +11,7 @@
use App\DependencyInjection\Compiler\PollutantCompilerPass;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
Expand Down Expand Up @@ -54,6 +56,8 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
$loader->load($confDir.'/services/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');

$container->addCompilerPass(new TwigSeoExtensionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);

$container->addCompilerPass(new PollutionLevelCompilerPass());
$container->registerForAutoconfiguration(PollutionLevelInterface::class)->addTag('pollution_level');

Expand Down
28 changes: 28 additions & 0 deletions src/Migrations/Version20181002172100.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20181002172100 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE city ADD latitude DOUBLE PRECISION NOT NULL, ADD longitude DOUBLE PRECISION NOT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE city DROP latitude, DROP longitude');
}
}
21 changes: 21 additions & 0 deletions src/SeoPage/SeoPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

namespace App\SeoPage;

use App\StaticMap\UrlGenerator\UrlGeneratorInterface;
use Sonata\SeoBundle\Seo\SeoPageInterface;

class SeoPage extends AbstractSeoPage
{
/** @var SeoPageInterface */
protected $sonataSeoPage;

/** @var UrlGeneratorInterface $urlGenerator */
protected $urlGenerator;

public function __construct(SeoPageInterface $sonataSeoPage, UrlGeneratorInterface $urlGenerator)
{
$this->sonataSeoPage = $sonataSeoPage;
$this->urlGenerator = $urlGenerator;
}

public function setTitle(string $title): SeoPageInterface
{
$this->sonataSeoPage
Expand Down Expand Up @@ -32,6 +47,7 @@ public function setStandardPreviewPhoto(): SeoPageInterface
return $this;
}

public function setPreviewMap(StaticMapableInterface $staticMapable): SeoPage
public function setOpenGraphPreviewPhoto(string $assetUrl): SeoPageInterface
{
$this->sonataSeoPage->addMeta('property', 'og:image', $this->asset($assetUrl));
Expand All @@ -42,6 +58,11 @@ public function setOpenGraphPreviewPhoto(string $assetUrl): SeoPageInterface
public function setTwitterPreviewPhoto(string $assetUrl): SeoPageInterface
{
$this->sonataSeoPage
->addMeta('property', 'og:image', $this->urlGenerator->generate($staticMapable, 600, 315),
['escape' => false])
->addMeta('name', 'twitter:image', $this->urlGenerator->generate($staticMapable, 800, 320),
['escape' => false])
->addMeta('name', 'twitter:card', 'summary_large_image');
->addMeta('name', 'twitter:image', $this->asset($assetUrl))
->addMeta('name', 'twitter:card', 'summary_large_image');

Expand Down
7 changes: 7 additions & 0 deletions src/StaticMap/StaticMapableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace App\StaticMap;

interface StaticMapableInterface
{
}
19 changes: 19 additions & 0 deletions src/StaticMap/UrlGenerator/AbstractUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace App\StaticMap\UrlGenerator;

abstract class AbstractUrlGenerator implements UrlGeneratorInterface
{
/** @var string $staticmapsHost */
protected $staticmapsHost = '';

/** @var array $defaultParameters */
protected $defaultParameters = [
'size' => '865x512',
];

public function __construct(string $staticmapsHost)
{
$this->staticmapsHost = $staticmapsHost;
}
}
Loading