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

[SCP-4641] feat: Add support to new endpoint get content score rules #193

Merged
merged 2 commits into from
Sep 16, 2024
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
19 changes: 19 additions & 0 deletions src/Factory/Xml/Category/CategoryContentScoreRuleConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Factory\Xml\Category;

use Linio\SellerCenter\Model\Category\CategoryContentScoreRuleConfig;
use SimpleXMLElement;

class CategoryContentScoreRuleConfigFactory
{
public static function make(SimpleXMLElement $element): CategoryContentScoreRuleConfig
{
return new CategoryContentScoreRuleConfig(
!empty($element->min) ? (int) $element->min : null,
!empty($element->max) ? (int) $element->max : null
);
}
}
40 changes: 40 additions & 0 deletions src/Factory/Xml/Category/CategoryContentScoreRuleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Factory\Xml\Category;

use Linio\SellerCenter\Exception\InvalidXmlStructureException;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRule;
use SimpleXMLElement;

class CategoryContentScoreRuleFactory
{
public static function make(SimpleXMLElement $element): CategoryContentScoreRule
{
if (!property_exists($element, 'Rule')) {
throw new InvalidXmlStructureException('CategoryContentScoreRule', 'Rule');
}

if (!property_exists($element, 'Field')) {
throw new InvalidXmlStructureException('CategoryContentScoreRule', 'Field');
}

if (!property_exists($element, 'Score')) {
throw new InvalidXmlStructureException('CategoryContentScoreRule', 'Score');
}

if (!property_exists($element, 'Config')) {
throw new InvalidXmlStructureException('CategoryContentScoreRule', 'Config');
}

$config = CategoryContentScoreRuleConfigFactory::make($element->Config);

return new CategoryContentScoreRule(
(string) $element->Rule,
(string) $element->Field,
(int) $element->Score,
$config
);
}
}
30 changes: 30 additions & 0 deletions src/Factory/Xml/Category/CategoryContentScoreRulesFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Factory\Xml\Category;

use Linio\SellerCenter\Exception\InvalidXmlStructureException;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRules;
use SimpleXMLElement;

class CategoryContentScoreRulesFactory
{
public static function make(SimpleXMLElement $element): CategoryContentScoreRules
{
if (empty($element->CategoryRules->Config)) {
throw new InvalidXmlStructureException('CategoryContentScoreRules', 'Config');
}

$categoryContentScoreRules = new CategoryContentScoreRules();

foreach ($element->CategoryRules->Config as $item) {
if (!empty($item)) {
$categoryContentScoreRule = CategoryContentScoreRuleFactory::make($item);
$categoryContentScoreRules->add($categoryContentScoreRule);
}
}

return $categoryContentScoreRules;
}
}
70 changes: 70 additions & 0 deletions src/Model/Category/CategoryContentScoreRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Model\Category;

use JsonSerializable;
use stdClass;

class CategoryContentScoreRule implements JsonSerializable
{
/**
* @var string
*/
protected $rule;

/**
* @var string
*/
protected $field;

/**
* @var int
*/
protected $score;

/**
* @var CategoryContentScoreRuleConfig
*/
protected $config;

public function __construct(string $rule, string $field, int $score = null, ?CategoryContentScoreRuleConfig $config = null)
{
$this->rule = $rule;
$this->field = $field;
$this->score = $score;
$this->config = $config;
}

public function getRule(): string
{
return $this->rule;
}

public function getField(): string
{
return $this->field;
}

public function getScore(): int
{
return $this->score;
}

public function getConfig(): ?CategoryContentScoreRuleConfig
{
return $this->config;
}

public function jsonSerialize(): stdClass
{
$serialized = new stdClass();
$serialized->rule = $this->rule;
$serialized->field = $this->field;
$serialized->score = $this->score;
$serialized->config = $this->config->jsonSerialize();

return $serialized;
}
}
46 changes: 46 additions & 0 deletions src/Model/Category/CategoryContentScoreRuleConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Model\Category;

use JsonSerializable;
use stdClass;

class CategoryContentScoreRuleConfig implements JsonSerializable
{
/**
* @var int|null
*/
protected $min;

/**
* @var int|null
*/
protected $max;

public function __construct(?int $min = null, ?int $max = null)
{
$this->min = $min;
$this->max = $max;
}

public function getMin(): ?int
{
return $this->min;
}

public function getMax(): ?int
{
return $this->max;
}

public function jsonSerialize(): stdClass
{
$serialized = new stdClass();
$serialized->min = $this->min;
$serialized->max = $this->max;

return $serialized;
}
}
28 changes: 28 additions & 0 deletions src/Model/Category/CategoryContentScoreRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Model\Category;

use Linio\SellerCenter\Contract\CollectionInterface;

class CategoryContentScoreRules implements CollectionInterface
{
/**
* @var CategoryContentScoreRule[]
*/
protected $collection;

/**
* @return CategoryContentScoreRule[]
*/
public function all(): array
{
return $this->collection;
}

public function add(CategoryContentScoreRule $categoryContentScoreRule): void
{
$this->collection[] = $categoryContentScoreRule;
}
}
30 changes: 30 additions & 0 deletions src/Service/CategoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
use Linio\SellerCenter\Factory\Xml\Category\AttributesSetFactory;
use Linio\SellerCenter\Factory\Xml\Category\CategoriesFactory;
use Linio\SellerCenter\Factory\Xml\Category\CategoryAttributesFactory;
use Linio\SellerCenter\Factory\Xml\Category\CategoryContentScoreRulesFactory;
use Linio\SellerCenter\Model\Category\AttributeSet;
use Linio\SellerCenter\Model\Category\Category;
use Linio\SellerCenter\Model\Category\CategoryAttribute;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRules;

class CategoryManager extends BaseManager
{
public const GET_RULES_ONLY_DEFAULT_VALUE = true;

/**
* @return Category[]
*/
Expand Down Expand Up @@ -93,4 +97,30 @@ public function getCategoriesByAttributesSet(

return $attributesSet->all();
}

public function getCategoryContentScoreRules(
int $categoryId,
?bool $getRulesOnly = self::GET_RULES_ONLY_DEFAULT_VALUE,
?string $operator = null,
bool $debug = true
): CategoryContentScoreRules {
$action = 'GetContentScore';

$parameters = $this->makeParametersForAction($action);
$parameters->set([
'CategoryId' => $categoryId,
'GetRulesOnly' => $getRulesOnly,
'Operator' => $operator,
]);

$builtResponse = $this->executeAction(
$action,
$parameters,
null,
'GET',
$debug
);

return CategoryContentScoreRulesFactory::make($builtResponse->getBody());
}
}
60 changes: 60 additions & 0 deletions tests/Functional/CategoryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Linio\SellerCenter\ClientHelper;
use Linio\SellerCenter\LinioTestCase;
use Linio\SellerCenter\Model\Category\Category;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRule;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRules;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -69,6 +71,64 @@ public function testTheCategoriesWillBeCreatedFromAnXml(): void
$this->assertEquals(2, $child_2->getAttributeSetId());
}

/**
* @dataProvider parametersProvider
*/
public function testTheCategoryContentScoreRulesWillBeCreatedFromAnXml(?bool $getRulesOnly, ?string $operator): void
{
$body = $this->getSchema('Category/GetCategoryContentScoreRulesSuccessResponse.xml');
$sdk = $this->getSdkClient($body);

$categoryId = 1;

$result = $sdk->categories()->getCategoryContentScoreRules($categoryId, $getRulesOnly, $operator);

$this->assertInstanceOf(CategoryContentScoreRules::class, $result);
$this->assertContainsOnlyInstancesOf(CategoryContentScoreRule::class, $result->all());

$rules = ['CHARACTER_COUNT', 'WORD_COUNT'];
$fields = ['title', 'description'];
$scores = [44, 44];
$mins = [20, 50];
$maxs = [60, null];

for ($i = 0; $i < 2; $i++) {
$categoryContentScoreRule = $result->all()[$i];

$this->assertEquals($rules[$i], $categoryContentScoreRule->getRule());
$this->assertEquals($fields[$i], $categoryContentScoreRule->getField());
$this->assertEquals($scores[$i], $categoryContentScoreRule->getScore());

$config = $categoryContentScoreRule->getConfig();

$this->assertEquals($mins[$i], $config->getMin());
$this->assertEquals($maxs[$i], $config->getMax());
}
}

public function parametersProvider()
{
return [
'Without optional parameters' => [
'getRulesOnly' => null,
'operator' => null,
],

'With first parameter only' => [
'getRulesOnly' => true,
'operator' => null,
],
'With second parameter only' => [
'getRulesOnly' => null,
'operator' => 'facl',
],
'With both parameters' => [
'getRulesOnly' => false,
'operator' => 'faco',
],
];
}

/**
* @dataProvider debugParameter
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Category/CategoryContentScoreRuleConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Linio\SellerCenter\Category;

use Linio\SellerCenter\Factory\Xml\Category\CategoryContentScoreRuleConfigFactory;
use Linio\SellerCenter\LinioTestCase;
use Linio\SellerCenter\Model\Category\CategoryContentScoreRuleConfig;

class CategoryContentScoreRuleConfigTest extends LinioTestCase
{
public function testItReturnsCategoryContentScoreRuleConfigObject(): void
{
$success = $this->getSchema('Category/GetCategoryContentScoreRulesSuccessResponse.xml');

$xml = simplexml_load_string($success);
$categoryContentScoreRuleConfig = CategoryContentScoreRuleConfigFactory::make($xml->Body->CategoryRules->Config->Config);

$this->assertNotEmpty($categoryContentScoreRuleConfig);
$this->assertInstanceOf(CategoryContentScoreRuleConfig::class, $categoryContentScoreRuleConfig);
}
}
Loading
Loading