Skip to content

Commit

Permalink
Add metadata in the results and refactor score classes accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidasmi committed Nov 25, 2024
1 parent bba6bd8 commit 7da1cda
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 312 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Application\Score_Results;

use Yoast\WP\SEO\Dashboard\Domain\Content_Types\Content_Type;
use Yoast\WP\SEO\Dashboard\Domain\Scores\Scores_Interface;
use Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score;

Check failure on line 8 in src/dashboard/application/score-results/abstract-score-results-repository.php

View workflow job for this annotation

GitHub Actions / Check code style

Use statements should be sorted alphabetically. The first wrong one is Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Score.
use Yoast\WP\SEO\Dashboard\Domain\Score_Results\Current_Scores_List;
use Yoast\WP\SEO\Dashboard\Domain\Score_Results\Score_Result;
use Yoast\WP\SEO\Dashboard\Domain\Taxonomies\Taxonomy;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Score_Results_Collector_Interface;
use Yoast\WP\SEO\Dashboard\Infrastructure\Scores\Score_Link_Collector;

/**
* The abstract score results repository.
*/
abstract class Abstract_Score_Results_Repository {

/**
* The score results collector.
*
* @var Score_Results_Collector_Interface
*/
protected $score_results_collector;

/**
* The score link collector.
*
* @var Score_Link_Collector
*/
protected $score_link_collector;

/**
* All scores.
*
* @var Scores_Interface[]
*/
protected $scores;

/**
* Sets the score link collector.
*
* @required
*
* @param Score_Link_Collector $score_link_collector The score link collector.
*
* @return void
*/
public function set_score_link_collector(
Score_Link_Collector $score_link_collector
) {
$this->score_link_collector = $score_link_collector;
}

/**
* Returns the score results for a content type.
*
* @param Content_Type $content_type The content type.
* @param Taxonomy|null $taxonomy The taxonomy of the term we're filtering for.
* @param int|null $term_id The ID of the term we're filtering for.
*
* @return array<array<string, string|int|array<string, string>>> The scores.
*/
public function get_score_results( Content_Type $content_type, ?Taxonomy $taxonomy, ?int $term_id ): array {
$current_scores_list = new Current_Scores_List();
$current_scores = $this->score_results_collector->get_current_scores( $this->scores, $content_type, $term_id );

foreach ( $this->scores as $score ) {
$score_name = $score->get_name();
$current_score_links = [
'view' => $this->score_link_collector->get_view_link( $score, $content_type, $taxonomy, $term_id ),
];

$current_score = new Current_Score( $score_name, (int) $current_scores->scores->$score_name, $current_score_links );
$current_scores_list->add( $current_score );
}

$score_result = new Score_Result( $current_scores_list, $current_scores->query_time, $current_scores->cache_used );

return $score_result->to_array();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Application\Score_Results\Readability_Score_Results;

use Yoast\WP\SEO\Dashboard\Application\Score_Results\Abstract_Score_Results_Repository;
use Yoast\WP\SEO\Dashboard\Domain\Scores\Readability_Scores\Readability_Scores_Interface;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\Readability_Score_Results\Readability_Score_Results_Collector;

/**
* The repository to get readability score results.
*/
class Readability_Score_Results_Repository extends Abstract_Score_Results_Repository {

/**
* The constructor.
*
* @param Readability_Score_Results_Collector $readability_score_results_collector The readability score results collector.
* @param Readability_Scores_Interface ...$readability_scores All readability scores.
*/
public function __construct(
Readability_Score_Results_Collector $readability_score_results_collector,
Readability_Scores_Interface ...$readability_scores
) {
$this->score_results_collector = $readability_score_results_collector;
$this->scores = $readability_scores;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
namespace Yoast\WP\SEO\Dashboard\Application\Score_Results\SEO_Score_Results;

use Yoast\WP\SEO\Dashboard\Application\Score_Results\Abstract_Score_Results_Repository;
use Yoast\WP\SEO\Dashboard\Domain\Scores\SEO_Scores\SEO_Scores_Interface;
use Yoast\WP\SEO\Dashboard\Infrastructure\Score_Results\SEO_Score_Results\SEO_Score_Results_Collector;

/**
* The repository to get SEO score results.
*/
class SEO_Score_Results_Repository extends Abstract_Score_Results_Repository {

/**
* The constructor.
*
* @param SEO_Score_Results_Collector $seo_score_results_collector The SEO score results collector.
* @param SEO_Scores_Interface ...$seo_scores All SEO scores.
*/
public function __construct(
SEO_Score_Results_Collector $seo_score_results_collector,
SEO_Scores_Interface ...$seo_scores
) {
$this->score_results_collector = $seo_score_results_collector;
$this->scores = $seo_scores;
}
}
77 changes: 0 additions & 77 deletions src/dashboard/application/scores/abstract-scores-repository.php

This file was deleted.

This file was deleted.

24 changes: 0 additions & 24 deletions src/dashboard/application/scores/scores-repository-interface.php

This file was deleted.

This file was deleted.

79 changes: 79 additions & 0 deletions src/dashboard/domain/score-results/current-score.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Dashboard\Domain\Score_Results;

/**
* This class describes a current score.
*/
class Current_Score {

/**
* The name of the current score.
*
* @var string
*/
private $name;

/**
* The amount of the current score.
*
* @var string
*/
private $amount;

/**
* The links of the current score.
*
* @var array<string, string>
*/
private $links;

/**
* The constructor.
*
* @param string $name The name of the current score.
* @param int $amount The amount of the current score.
* @param array<string, string> $links The links of the current score.
*/
public function __construct( string $name, int $amount, ?array $links = null ) {
$this->name = $name;
$this->amount = $amount;
$this->links = $links;
}

/**
* Gets name of the current score.
*
* @return string The name of the current score.
*/
public function get_name(): string {
return $this->name;
}

/**
* Gets the amount of the current score.
*
* @return string The amount of the current score.
*/
public function get_amount(): int {
return $this->amount;
}

/**
* Gets the links of the current score in the expected key value representation.
*
* @return array<string,string> The links of the current score in the expected key value representation.
*/
public function get_links_to_array(): ?array {
$links = [];

if ( $this->links === null ) {
return $links;
}

foreach ( $this->links as $key => $link ) {
$links[ $key ] = $link;
}
return $links;
}
}
Loading

0 comments on commit 7da1cda

Please sign in to comment.