/**
 * Get an activation record by its location.
 *
 * @api
 *
 * @since 1.0
 *
 * @param string      $location
 * @param \ITELIC\Key $key
 *
 * @return \ITELIC\Activation|null
 */
function itelic_get_activation_by_location($location, \ITELIC\Key $key)
{
    $activations = itelic_get_activations(array('location' => $key->is_online_product() ? itelic_normalize_url($location) : $location, 'key' => $key->get_key(), 'items_per_page' => 1));
    if (empty($activations)) {
        return null;
    }
    return reset($activations);
}
 public function test_location_is_properly_saved()
 {
     $activate = new \ITELIC\API\Endpoint\Activate();
     $key = $this->key_factory->create_and_get(array('product' => $this->product_factory->create(), 'customer' => 1));
     $activate->set_auth_license_key($key);
     $location = 'www.test.com';
     $response = $activate->serve(new ArrayObject(), new ArrayObject(array('location' => $location)));
     $data = $response->get_data();
     $this->assertTrue($data['success']);
     $this->assertEquals(itelic_normalize_url($location), $data['body']->get_location());
 }
 /**
  * Create an activation.
  *
  * @param Key       $key
  * @param string    $location
  * @param \DateTime $activation
  * @param Release   $release
  * @param string    $status
  *
  * @return Activation
  *
  * @throws \LogicException|DB_Exception
  */
 public static function create(Key $key, $location, \DateTime $activation = null, Release $release = null, $status = '')
 {
     if (empty($key) || empty($location)) {
         throw new \InvalidArgumentException(__("The license key and install location are required.", Plugin::SLUG));
     }
     if (strlen($location) > 191) {
         throw new \LengthException("The location field has a max length of 191 characters.");
     }
     if ($key->get_max() && $key->get_active_count() >= $key->get_max()) {
         throw new \OverflowException(__("This license key has reached it's maximum number of activations.", Plugin::SLUG));
     }
     if ($activation === null) {
         $activation = make_date_time()->format('Y-m-d H:i:s');
     } else {
         $activation = $activation->format('Y-m-d H:i:s');
     }
     if (empty($status)) {
         $status = self::ACTIVE;
     }
     if ($key->is_online_product()) {
         $location = itelic_normalize_url($location);
     }
     $data = array('lkey' => $key->get_key(), 'location' => $location, 'activation' => $activation, 'deactivation' => null, 'status' => $status);
     if ($release) {
         $data['release_id'] = $release->get_pk();
     }
     $db = Manager::make_simple_query_object('itelic-activations');
     $existing_activation = itelic_get_activation_by_location($location, $key);
     if ($existing_activation) {
         throw new \InvalidArgumentException(__("An activation with this same location already exists.", Plugin::SLUG));
     }
     $id = $db->insert($data);
     if (!$id) {
         return null;
     }
     $activation = self::get($id);
     Cache::add($activation);
     if (!$release) {
         $latest = $key->get_product()->get_latest_release_for_activation($activation);
         if ($latest) {
             $activation->set_release($latest);
         }
     }
     /**
      * Fires when an activation record is created.
      *
      * @since 1.0
      *
      * @param Activation $activation
      */
     do_action('itelic_create_activation', $activation);
     return $activation;
 }