$value]); if (strlen($value) === 0) { $this->setCertainty(0); return new Category; } /** @var CategoryRepositoryInterface $repository */ $repository = app(CategoryRepositoryInterface::class, [$this->user]); if (isset($this->mapping[$value])) { Log::debug('Found category in mapping. Should exist.', ['value' => $value, 'map' => $this->mapping[$value]]); $category = $repository->find(intval($this->mapping[$value])); if (!is_null($category->id)) { Log::debug('Found category by ID', ['id' => $category->id]); $this->setCertainty(100); return $category; } } // not mapped? Still try to find it first: $category = $repository->findByName($value); if (!is_null($category->id)) { Log::debug('Found category by name ', ['id' => $category->id]); $this->setCertainty(100); return $category; } // create new category. Use a lot of made up values. $category = $repository->store( [ 'name' => $value, 'user' => $this->user->id, ] ); $this->setCertainty(100); return $category; } }