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

Move some namespaces on generator #805

Merged
merged 5 commits into from
Dec 9, 2023
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ parameters:
- %currentWorkingDirectory%/src/Component/Annotation/*
- %currentWorkingDirectory%/src/Component/Factory/*
- %currentWorkingDirectory%/src/Component/Metadata/*
- %currentWorkingDirectory%/src/Component/Generator/*
- %currentWorkingDirectory%/src/Component/Reflection/ClassReflection.php
- %currentWorkingDirectory%/src/Component/spec/*
- %currentWorkingDirectory%/src/Component/Storage/*
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<directory name="src/Component/Annotation" />
<directory name="src/Component/Factory" />
<directory name="src/Component/Metadata" />
<directory name="src/Component/Generator" />
<directory name="src/Component/Storage" />
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
Expand All @@ -220,6 +221,7 @@
<errorLevel type="suppress">
<directory name="src/Component/Factory" />
<directory name="src/Component/Metadata" />
<directory name="src/Component/Generator" />
<directory name="src/Component/Storage" />
</errorLevel>
</UnrecognizedStatement>
Expand Down
7 changes: 5 additions & 2 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
<services>
<defaults public="true" />

<service id="sylius.random_generator" class="Sylius\Component\Resource\Generator\RandomnessGenerator" />
<service id="Sylius\Component\Resource\Generator\RandomnessGeneratorInterface" alias="sylius.random_generator" />
<service id="sylius.random_generator" class="Sylius\Resource\Generator\RandomnessGenerator" />
<service id="Sylius\Resource\Generator\RandomnessGeneratorInterface" alias="sylius.random_generator" />
<service id="Sylius\Component\Resource\Generator\RandomnessGeneratorInterface" alias="sylius.random_generator">
<deprecated package="sylius/resource-bundle" version="1.11">The "%alias_id%" service alias is deprecated since sylius/resource-bundle 1.11 and will be removed in sylius/resource-bundle 2.0. Use Sylius\Resource\Generator\RandomnessGeneratorInterface instead.</deprecated>
</service>

<service id="sylius.form.type_extension.form.request_handler"
class="Sylius\Bundle\ResourceBundle\Form\Extension\HttpFoundation\HttpFoundationRequestHandler"
Expand Down
50 changes: 3 additions & 47 deletions src/Component/Generator/RandomnessGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,10 @@

namespace Sylius\Component\Resource\Generator;

use Webmozart\Assert\Assert;
class_exists(\Sylius\Resource\Generator\RandomnessGenerator::class);

final class RandomnessGenerator implements RandomnessGeneratorInterface
{
private string $uriSafeAlphabet;

private string $digits;

public function __construct()
{
$this->digits = implode(range(0, 9));

$this->uriSafeAlphabet =
implode(range(0, 9))
. implode(range('a', 'z'))
. implode(range('A', 'Z'))
. implode(['-', '_', '~'])
;
}

public function generateUriSafeString(int $length): string
if (false) {
final class RandomnessGenerator extends \Sylius\Resource\Generator\RandomnessGenerator
{
return $this->generateStringOfLength($length, $this->uriSafeAlphabet);
}

public function generateNumeric(int $length): string
{
return $this->generateStringOfLength($length, $this->digits);
}

public function generateInt(int $min, int $max): int
{
return random_int($min, $max);
}

private function generateStringOfLength(int $length, string $alphabet): string
{
$alphabetMaxIndex = strlen($alphabet) - 1;

Assert::greaterThanEq($alphabetMaxIndex, 1);

$randomString = '';

for ($i = 0; $i < $length; ++$i) {
$index = random_int(0, $alphabetMaxIndex);
$randomString .= $alphabet[$index];
}

return $randomString;
}
}
11 changes: 5 additions & 6 deletions src/Component/Generator/RandomnessGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

namespace Sylius\Component\Resource\Generator;

interface RandomnessGeneratorInterface
{
public function generateUriSafeString(int $length): string;
interface_exists(\Sylius\Resource\Generator\RandomnessGeneratorInterface::class);

public function generateNumeric(int $length): string;

public function generateInt(int $min, int $max): int;
if (false) {
interface RandomnessGeneratorInterface extends \Sylius\Resource\Generator\RandomnessGeneratorInterface
{
}
}
40 changes: 7 additions & 33 deletions src/Component/spec/Generator/RandomnessGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace spec\Sylius\Component\Resource\Generator;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface;
use Sylius\Component\Resource\Generator\RandomnessGeneratorInterface as LegacyRandomnessGeneratorInterface;
use Sylius\Resource\Generator\RandomnessGenerator as NewRandomnessGenerator;
use Sylius\Resource\Generator\RandomnessGeneratorInterface;

final class RandomnessGeneratorSpec extends ObjectBehavior
{
Expand All @@ -23,41 +25,13 @@ function it_implements_randomness_generator_interface(): void
$this->shouldImplement(RandomnessGeneratorInterface::class);
}

function it_generates_random_uri_safe_string_of_length(): void
function it_implements_legacy_randomness_generator_interface(): void
{
$length = 9;

$this->generateUriSafeString($length)->shouldBeString();
$this->generateUriSafeString($length)->shouldHaveLength($length);
}

function it_generates_random_numeric_string_of_length(): void
{
$length = 12;

$this->generateNumeric($length)->shouldBeString();
$this->generateNumeric($length)->shouldBeNumeric();
$this->generateNumeric($length)->shouldHaveLength($length);
}

function it_generates_random_int_in_range(): void
{
$min = 12;
$max = 2000000;

$this->generateInt($min, $max)->shouldBeInt();
$this->generateInt($min, $max)->shouldBeInRange($min, $max);
$this->shouldImplement(LegacyRandomnessGeneratorInterface::class);
}

public function getMatchers(): array
function it_should_be_an_alias_of_randomness_generator(): void
{
return [
'haveLength' => function ($subject, $length) {
return $length === strlen($subject);
},
'beInRange' => function ($subject, $min, $max) {
return $subject >= $min && $subject <= $max;
},
];
$this->shouldBeAnInstanceOf(NewRandomnessGenerator::class);
}
}
68 changes: 68 additions & 0 deletions src/Component/src/Generator/RandomnessGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Generator;

use Webmozart\Assert\Assert;

final class RandomnessGenerator implements RandomnessGeneratorInterface
{
private string $uriSafeAlphabet;

private string $digits;

public function __construct()
{
$this->digits = implode(range(0, 9));
diimpp marked this conversation as resolved.
Show resolved Hide resolved

$this->uriSafeAlphabet =
implode(range(0, 9))
. implode(range('a', 'z'))
. implode(range('A', 'Z'))
. implode(['-', '_', '~'])
;
}

public function generateUriSafeString(int $length): string
{
return $this->generateStringOfLength($length, $this->uriSafeAlphabet);
}

public function generateNumeric(int $length): string
{
return $this->generateStringOfLength($length, $this->digits);
}

public function generateInt(int $min, int $max): int
{
return random_int($min, $max);
}

private function generateStringOfLength(int $length, string $alphabet): string
{
$alphabetMaxIndex = strlen($alphabet) - 1;

Assert::greaterThanEq($alphabetMaxIndex, 1);

$randomString = '';

for ($i = 0; $i < $length; ++$i) {
$index = random_int(0, $alphabetMaxIndex);
$randomString .= $alphabet[$index];
}

return $randomString;
}
}

class_alias(RandomnessGenerator::class, \Sylius\Component\Resource\Generator\RandomnessGenerator::class);
25 changes: 25 additions & 0 deletions src/Component/src/Generator/RandomnessGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Resource\Generator;

interface RandomnessGeneratorInterface
{
public function generateUriSafeString(int $length): string;

public function generateNumeric(int $length): string;

public function generateInt(int $min, int $max): int;
}

class_alias(RandomnessGeneratorInterface::class, \Sylius\Component\Resource\Generator\RandomnessGeneratorInterface::class);
63 changes: 63 additions & 0 deletions src/Component/tests/spec/Generator/RandomnessGeneratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Resource\Generator;

use PhpSpec\ObjectBehavior;
use Sylius\Resource\Generator\RandomnessGeneratorInterface;

final class RandomnessGeneratorSpec extends ObjectBehavior
{
function it_implements_randomness_generator_interface(): void
{
$this->shouldImplement(RandomnessGeneratorInterface::class);
}

function it_generates_random_uri_safe_string_of_length(): void
{
$length = 9;

$this->generateUriSafeString($length)->shouldBeString();
$this->generateUriSafeString($length)->shouldHaveLength($length);
}

function it_generates_random_numeric_string_of_length(): void
{
$length = 12;

$this->generateNumeric($length)->shouldBeString();
$this->generateNumeric($length)->shouldBeNumeric();
$this->generateNumeric($length)->shouldHaveLength($length);
}

function it_generates_random_int_in_range(): void
{
$min = 12;
$max = 2000000;

$this->generateInt($min, $max)->shouldBeInt();
$this->generateInt($min, $max)->shouldBeInRange($min, $max);
}

public function getMatchers(): array
{
return [
'haveLength' => function ($subject, $length) {
return $length === strlen($subject);
},
'beInRange' => function ($subject, $min, $max) {
return $subject >= $min && $subject <= $max;
},
];
}
}
Loading