Whip_RequirementsChecker

Main controller class to require a certain version of software.

Defined (1)

The class is defined in the following location(s).

/vendor/yoast/whip/src/Whip_RequirementsChecker.php  
  1. class Whip_RequirementsChecker { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. private $requirements; 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. private $textdomain; 
  12.  
  13. /** 
  14. * Whip_RequirementsChecker constructor. 
  15. * @param array $configuration The configuration to check. 
  16. * @param string $textdomain The text domain to use for translations. 
  17. */ 
  18. public function __construct( $configuration = array(), $textdomain = 'wordpress-seo' ) { 
  19. $this->requirements = array(); 
  20. $this->configuration = new Whip_Configuration( $configuration ); 
  21. $this->messageMananger = new Whip_MessagesManager(); 
  22. $this->textdomain = $textdomain; 
  23.  
  24. /** 
  25. * Adds a requirement to the list of requirements if it doesn't already exist. 
  26. * @param Whip_Requirement $requirement The requirement to add. 
  27. */ 
  28. public function addRequirement( Whip_Requirement $requirement ) { 
  29. // Only allow unique entries to ensure we're not checking specific combinations multiple times 
  30. if ( $this->requirementExistsForComponent( $requirement->component() ) ) { 
  31. return; 
  32.  
  33. $this->requirements[] = $requirement; 
  34.  
  35. /** 
  36. * Determines whether or not there are requirements available. 
  37. * @return bool Whether or not there are requirements. 
  38. */ 
  39. public function hasRequirements() { 
  40. return $this->totalRequirements() > 0; 
  41.  
  42. /** 
  43. * Gets the total amount of requirements. 
  44. * @return int The total amount of requirements. 
  45. */ 
  46. public function totalRequirements() { 
  47. return count( $this->requirements ); 
  48.  
  49. /** 
  50. * Determines whether or not a requirement exists for a particular component. 
  51. * @param string $component The component to check for. 
  52. * @return bool Whether or not the component has a requirement defined. 
  53. */ 
  54. public function requirementExistsForComponent( $component ) { 
  55. foreach ( $this->requirements as $requirement ) { 
  56. if ( $requirement->component() === $component ) { 
  57. return true; 
  58.  
  59. return false; 
  60.  
  61. /** 
  62. * Determines whether a requirement has been fulfilled. 
  63. * @param Whip_Requirement $requirement The requirement to check. 
  64. * @return bool Whether or not the requirement is fulfilled. 
  65. */ 
  66. private function requirementIsFulfilled( Whip_Requirement $requirement ) { 
  67. $available_version = $this->configuration->configuredVersion( $requirement ); 
  68. $required_version = $requirement->version(); 
  69.  
  70. if ( in_array( $requirement->operator(), array( '=', '==', '===' ), true ) ) { 
  71. return -1 !== version_compare( $available_version, $required_version ); 
  72.  
  73. return version_compare( $available_version, $required_version, $requirement->operator() ); 
  74.  
  75. /** 
  76. * Checks if all requirements are fulfilled and adds a message to the message manager if necessary. 
  77. */ 
  78. public function check() { 
  79. foreach ( $this->requirements as $requirement ) { 
  80. // Match against config 
  81. $requirement_fulfilled = $this->requirementIsFulfilled( $requirement ); 
  82.  
  83. if ( $requirement_fulfilled ) { 
  84. continue; 
  85.  
  86. $this->addMissingRequirementMessage( $requirement ); 
  87.  
  88. /** 
  89. * Adds a message to the message manager for requirements that cannot be fulfilled. 
  90. * @param Whip_Requirement $requirement The requirement that cannot be fulfilled. 
  91. */ 
  92. private function addMissingRequirementMessage( Whip_Requirement $requirement ) { 
  93. switch ( $requirement->component() ) { 
  94. case 'php': 
  95. $this->messageMananger->addMessage( new Whip_UpgradePhpMessage( $this->textdomain ) ); 
  96. break; 
  97. default: 
  98. $this->messageMananger->addMessage( new Whip_InvalidVersionRequirementMessage( $requirement, $this->configuration->configuredVersion( $requirement ) ) ); 
  99. break; 
  100.  
  101. /** 
  102. * Determines whether or not there are messages available. 
  103. * @return bool Whether or not there are messages to display. 
  104. */ 
  105. public function hasMessages() { 
  106. return $this->messageMananger->hasMessages(); 
  107.  
  108. /** 
  109. * Gets the most recent message from the message manager. 
  110. * @return Whip_Message The latest message. 
  111. */ 
  112. public function getMostRecentMessage() { 
  113. return $this->messageMananger->getLatestMessage(); 
  114.