diff --git a/src/Plugin/Validation/Constraint/SafeSearchConstraintValidator.php b/src/Plugin/Validation/Constraint/SafeSearchConstraintValidator.php index 6703ab3..14881fc 100755 --- a/src/Plugin/Validation/Constraint/SafeSearchConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/SafeSearchConstraintValidator.php @@ -12,33 +12,52 @@ use Drupal\field\FieldConfigInterface; use Drupal\field\FieldStorageConfigInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; /** * Validates the SafeSearch constraint. */ -class SafeSearchConstraintValidator extends ConstraintValidator { +class SafeSearchConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface { + + /** + * The Google Vision API. + * + * @var \Drupal\google_vision\GoogleVisionAPI. + */ + protected $googlevisionapi; + + /** + * Constructs a SafeSearchConstraintValidator object. + * + * @param \Drupal\google_vision\GoogleVisionAPI $googlevisionapi + * The Google Vision APi object. + */ + public function __construct(GoogleVisionAPI $google_vision) { + $this->googlevisionapi = $google_vision; + } /** * {@inheritdoc} */ public function validate($data, Constraint $constraint) { - foreach(\Drupal::service('entity_field.manager')->getFieldDefinitions('node', $data->getType()) as $field_name => $field_def) { + foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions('node', $data->getType()) as $field_name => $field_def) { // if field is image field. - if($field_def->getType() == 'image') { + if ($field_def->getType() == 'image') { $settings = $field_def->getThirdPartySettings('google_vision'); // if the Safe Search detection is on. - if(!empty($settings['google_vision'])) { + if (!empty($settings['google_vision'])) { // if the image is uploaded. - if(!empty($data->get($field_name)->target_id)) { + if (!empty($data->get($field_name)->target_id)) { // Retrieve the file uri. $file_uri = $data->get($field_name)->entity->getFileUri(); - if($filepath = \Drupal::service('file_system')->realpath($file_uri)) { - $result = \Drupal::service('google_vision.api')->safeSearchDetection($filepath); - if(!empty($result['responses'][0]['safeSearchAnnotation'])) { + if ($filepath = \Drupal::service('file_system')->realpath($file_uri)) { + $result = $this->googlevisionapi->safeSearchDetection($filepath); + if (!empty($result['responses'][0]['safeSearchAnnotation'])) { $adult = $result['responses'][0]['safeSearchAnnotation']['adult']; $likelihood = array('LIKELY', 'VERY_LIKELY'); // if the image has explicit content. - if(in_array($adult, $likelihood)) { + if (in_array($adult, $likelihood)) { $this->context->addViolation($constraint->message, array('%title' => $data->get($field_name)->entity->getFilename())); } } @@ -48,4 +67,12 @@ class SafeSearchConstraintValidator extends ConstraintValidator { } } } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static ($container->get('google_vision.api') + ); + } }