Skip to content

Commit

Permalink
Move service params to constructors
Browse files Browse the repository at this point in the history
Don't use setter methods, will make services.neon a bit nicer and shorter.
  • Loading branch information
spaze committed May 15, 2022
1 parent 7efdea4 commit 568a25d
Show file tree
Hide file tree
Showing 22 changed files with 134 additions and 466 deletions.
62 changes: 12 additions & 50 deletions site/app/Application/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ class RouterFactory

private const ROOT_ONLY = '';

/** @var array<string, array<string, string>> of host => array of supported locales */
private array $supportedLocales;

/** @var array<string, string> of locale => root domain */
private array $rootDomainMapping;

/** @var array<string, array<string, array{mask:array<string, string>, actions?:array<string, array<string, string>>}>> */
private array $translatedRoutes;

/** @var array<string, array<string, array<string, string>>> */
private array $translatedPresenters = [];

Expand All @@ -73,54 +64,25 @@ class RouterFactory
private array $availableLocales;


public function __construct(
private Loader $blogPostLoader,
private Translator $translator,
) {
$this->availableLocales = $this->translator->getAvailableLocales();
}


/**
* Set supported locales
*
* @param array<string, array<string, string>> $supportedLocales array of host => array of supported locales
*/
public function setSupportedLocales(array $supportedLocales): void
{
$this->supportedLocales = $supportedLocales;
}


/**
* Set locale to root domain mapping.
*
* @param array<string, array<string, string>> $supportedLocales host => array of supported locales
* @param array<string, string> $rootDomainMapping locale => root domain
* @param array<string, array<string, array{mask:array<string, string>, actions?:array<string, array<string, string>>}>> $translatedRoutes
*/
public function setLocaleRootDomainMapping(array $rootDomainMapping): void
{
$this->rootDomainMapping = $rootDomainMapping;
}


/**
* Get locale to root domain mapping.
*
* @return array<string, string> $rootDomainMapping locale => root domain
*/
public function getLocaleRootDomainMapping(): array
{
return $this->rootDomainMapping;
public function __construct(
private readonly Loader $blogPostLoader,
private readonly Translator $translator,
private readonly array $supportedLocales,
private readonly array $rootDomainMapping,
private readonly array $translatedRoutes,
) {
$this->availableLocales = $this->translator->getAvailableLocales();
$this->setTranslatedPresentersAndActions();
}


/**
* @param array<string, array<string, array{mask:array<string, string>, actions?:array<string, array<string, string>>}>> $translatedRoutes
*/
public function setTranslatedRoutes(array $translatedRoutes): void
private function setTranslatedPresentersAndActions(): void
{
$this->translatedRoutes = $translatedRoutes;

foreach ($this->translatedRoutes as $module => $routes) {
foreach ($routes as $presenter => $items) {
foreach ($items['mask'] as $locale => $mask) {
Expand Down
8 changes: 3 additions & 5 deletions site/app/CompanyInfo/Ares.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ class Ares implements CompanyDataInterface
*/
private const STATUS_FOUND = 1;

private string $url;


public function setUrl(string $url): void
{
$this->url = $url;
public function __construct(
private readonly string $url,
) {
}


Expand Down
16 changes: 6 additions & 10 deletions site/app/CompanyInfo/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,15 @@
class Info
{

private Ares $ares;

private RegisterUz $registerUz;

private Cache $cache;

private bool $loadCompanyDataVisible = true;


public function __construct(Ares $ares, RegisterUz $registerUz, Storage $cacheStorage)
{
$this->ares = $ares;
$this->registerUz = $registerUz;
public function __construct(
private readonly Ares $ares,
private readonly RegisterUz $registerUz,
Storage $cacheStorage,
private bool $loadCompanyDataVisible = true,
) {
$this->cache = new Cache($cacheStorage, self::class);
}

Expand Down
9 changes: 2 additions & 7 deletions site/app/CompanyInfo/RegisterUz.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ class RegisterUz implements CompanyDataInterface

private const COUNTRY_CODE = 'sk';

private string $rootUrl;
private readonly string $rootUrl;


/**
* Root URL of the service, ends with a slash.
*
* @param string $rootUrl
*/
public function setRootUrl(string $rootUrl): void
public function __construct(string $rootUrl)
{
if ($rootUrl[strlen($rootUrl) - 1] !== '/') {
$rootUrl .= '/';
Expand Down
41 changes: 7 additions & 34 deletions site/app/Formatter/TexyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,14 @@ public function __construct(
private readonly LocaleLinkGenerator $localeLinkGenerator,
private readonly LocaleUrls $blogPostLocaleUrls,
private readonly DateTimeFormatter $dateTimeFormatter,
string $staticRoot,
string $imagesRoot,
string $locationRoot,
) {
$this->cacheNamespace = 'TexyFormatted' . '.' . $this->translator->getLocale();
}


/**
* Set static content URL root.
*
* @param string $root
*/
public function setStaticRoot(string $root): void
{
$this->staticRoot = rtrim($root, '/');
$this->staticRoot = rtrim($staticRoot, '/');
$this->imagesRoot = trim($imagesRoot, '/');
$this->locationRoot = rtrim($locationRoot, '/');
$this->cacheNamespace = 'TexyFormatted.' . $this->translator->getLocale();
}


Expand All @@ -97,17 +92,6 @@ public function getStaticRoot(): string
}


/**
* Set images root directory.
*
* @param string $root
*/
public function setImagesRoot(string $root): void
{
$this->imagesRoot = trim($root, '/');
}


/**
* Get absolute URL of the image.
*
Expand All @@ -120,17 +104,6 @@ public function getImagesRoot(string $filename): string
}


/**
* Set location root directory.
*
* @param string $root
*/
public function setLocationRoot(string $root): void
{
$this->locationRoot = rtrim($root, '/');
}


/**
* Set top heading level.
*
Expand Down
24 changes: 10 additions & 14 deletions site/app/Http/SecurityHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,42 @@ class SecurityHeaders
private string $actionName;

/** @var array<string|string[]> */
private array $permissionsPolicy;
private readonly array $permissionsPolicy;


/**
* @param array<string|null|string[]> $permissionsPolicy
*/
public function __construct(
private readonly IRequest $httpRequest,
private readonly IResponse $httpResponse,
private readonly Config $contentSecurityPolicy,
private readonly LocaleLinkGenerator $localeLinkGenerator,
array $permissionsPolicy,
) {
}


/**
* @param array<string|null|string[]> $policy
*/
public function setPermissionsPolicy(array $policy): void
{
$permissionsPolicy = $policy;
$this->normalizePermissionsPolicyValues($permissionsPolicy);
$this->permissionsPolicy = $permissionsPolicy;
$this->permissionsPolicy = $this->normalizePermissionsPolicyValues($permissionsPolicy);
}


/**
* @param array<string|null|string[]> $values
* @return array<string|string[]>
*/
private function normalizePermissionsPolicyValues(array &$values): void
private function normalizePermissionsPolicyValues(array $values): array
{
foreach ($values as &$value) {
if ($value === 'none' || $value === null) {
$value = '';
} elseif (is_array($value)) {
$this->normalizePermissionsPolicyValues($value);
$value = $this->normalizePermissionsPolicyValues($value);
} elseif ($value !== 'self') {
$value = trim($value);
if ($value !== '') {
$value = sprintf('"%s"', $value);
}
}
}
return $values;
}


Expand Down
25 changes: 5 additions & 20 deletions site/app/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ class Post
/** @var string[]|null */
private ?array $locales = null;

private int $updatedInfoThreshold;

/** @var array<string, array<string, array<int, string>>> */
private array $allowedTags;


/**
* @param array<string, array<string, array<int, string>>> $allowedTags
*/
public function __construct(
private Explorer $database,
private Loader $loader,
Expand All @@ -41,6 +39,8 @@ public function __construct(
private LocaleLinkGenerator $localeLinkGenerator,
private Tags $tags,
private Translator $translator,
private readonly int $updatedInfoThreshold,
private readonly array $allowedTags,
) {
}

Expand All @@ -51,12 +51,6 @@ public function getUpdatedInfoThreshold(): int
}


public function setUpdatedInfoThreshold(int $updatedInfoThreshold): void
{
$this->updatedInfoThreshold = $updatedInfoThreshold;
}


/**
* @return array<string, array<string, array<int, string>>>
*/
Expand All @@ -66,15 +60,6 @@ public function getAllowedTags(): array
}


/**
* @param array<string, array<string, array<int, string>>> $allowedTags
*/
public function setAllowedTags(array $allowedTags): void
{
$this->allowedTags = $allowedTags;
}


/**
* @throws InvalidLinkException
* @throws JsonException
Expand Down
Loading

0 comments on commit 568a25d

Please sign in to comment.