src/Model/Table/TokensTable.php
public function newKey($type, $key = null, $uid = null, $content = null) {
		if (!$type) {
			return false;
		}

		if (!$key) {
			$key = $this->generateKey($this->defaultLength);
			$keyLength = $this->defaultLength;
		} else {
			$keyLength = mb_strlen($key);
		}

		if (is_array($content)) {
			$content = json_encode($content);
		}

		$data = [
			'type' => $type,
			'user_id' => $uid,
			'content' => (string)$content,
			'key' => $key,
		];

		$entity = $this->newEntity($data);
		$max = 99;
		while (!$this->save($entity)) {
			$entity['key'] = $this->generateKey($keyLength);
			$max--;
			if ($max === 0) {
				return false;
			}
		}

		return $entity['key'];
	}
public function newKey($type, $key = null, $uid = null, $content = null) {
		if (!$type) {
			return false;
		}

		if (!$key) {
			$key = $this->generateKey($this->defaultLength);
			$keyLength = $this->defaultLength;
		} else {
			$keyLength = mb_strlen($key);
		}

		if (is_array($content)) {
			$content = json_encode($content);
		}

		$data = [
			'type' => $type,
			'user_id' => $uid,
			'content' => (string)$content,
			'key' => $key,
		];

		$entity = $this->newEntity($data);
		$max = 99;
		while (!$this->save($entity)) {
			$entity['key'] = $this->generateKey($keyLength);
			$max--;
			if ($max === 0) {
				return false;
			}
		}

		return $entity['key'];
	}
public function newKey ( $ type , $ key = null , $ uid = null , $ content = null ) { if ( ! $ type ) { return false ; } if ( ! $ key ) { $ key = $ this -> generateKey ( $ this -> defaultLength ) ; $ keyLength = $ this -> defaultLength ; } else { $ keyLength = mb_strlen ( $ key ) ; } if ( is_array ( $ content ) ) { $ content = json_encode ( $ content ) ; } $ data = [ 'type' => $ type , 'user_id' => $ uid , 'content' => ( string ) $ content , 'key' => $ key , ] ; $ entity = $ this -> newEntity ( $ data ) ; $ max = 99 ; while ( ! $ this -> save ( $ entity ) ) { $ entity [ 'key' ] = $ this -> generateKey ( $ keyLength ) ; $ max -- ; if ( $ max === 0 ) { return false ; } } return $ entity [ 'key' ] ; }
Stores new key in DB

Checks if this key is already used (should be unique in table)

@param string $type Type: necessary
@param string|null $key Key: optional key, otherwise a key will be generated
@param mixed|null $uid Uid: optional (if used, only this user can use this key)
@param string|array|null $content Content: up to 255 characters of content may be added (optional)

@return string|bool Key on success, boolean false otherwise
Stores new key in DB