Code Duplication    Length = 48-48 lines in 2 locations

src/ServiceProvider.php 1 location

@@ 300-347 (lines=48) @@
297
     * @return string
298
     * @throws \UnexpectedValueException
299
     */
300
    protected function makeSlugUnique(string $slug, array $config, string $attribute): string
301
    {
302
        if (!$config['unique']) {
303
            return $slug;
304
        }
305
306
        $separator = $config['separator'];
307
308
        // find all models where the slug is like the current one
309
        $list = $this->getExistingSlugs($slug, $attribute, $config);
310
311
        // if ...
312
        //     a) the list is empty, or
313
        //     b) our slug isn't in the list
314
        // ... we are okay
315
        if (
316
            $list->count() === 0 ||
317
            $list->contains($slug) === false
318
        ) {
319
            return $slug;
320
        }
321
322
        // if our slug is in the list, but
323
        //     a) it's for our model, or
324
        //  b) it looks like a suffixed version of our slug
325
        // ... we are also okay (use the current slug)
326
        if ($list->has($this->model->getKey())) {
327
            $currentSlug = $list->get($this->model->getKey());
328
329
            if (
330
                $currentSlug === $slug ||
331
                !$slug || strpos($currentSlug, $slug) === 0
332
            ) {
333
                return $currentSlug;
334
            }
335
        }
336
337
        $method = $config['uniqueSuffix'];
338
        if ($method === null) {
339
            $suffix = $this->generateSuffix($slug, $separator, $list);
340
        } elseif (is_callable($method)) {
341
            $suffix = $method($slug, $separator, $list);
342
        } else {
343
            throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
344
        }
345
346
        return $slug . $separator . $suffix;
347
    }
348
349
    /**
350
     * Generate a unique suffix for the given slug (and list of existing, "similar" slugs.

src/Services/SlugService.php 1 location

@@ 286-333 (lines=48) @@
283
     * @return string
284
     * @throws \UnexpectedValueException
285
     */
286
    protected function makeSlugUnique(string $slug, array $config, string $attribute): string
287
    {
288
        if (!$config['unique']) {
289
            return $slug;
290
        }
291
292
        $separator = $config['separator'];
293
294
        // find all models where the slug is like the current one
295
        $list = $this->getExistingSlugs($slug, $attribute, $config);
296
297
        // if ...
298
        //     a) the list is empty, or
299
        //     b) our slug isn't in the list
300
        // ... we are okay
301
        if (
302
            $list->count() === 0 ||
303
            $list->contains($slug) === false
304
        ) {
305
            return $slug;
306
        }
307
308
        // if our slug is in the list, but
309
        //     a) it's for our model, or
310
        //  b) it looks like a suffixed version of our slug
311
        // ... we are also okay (use the current slug)
312
        if ($list->has($this->model->getKey())) {
313
            $currentSlug = $list->get($this->model->getKey());
314
315
            if (
316
                $currentSlug === $slug ||
317
                !$slug || strpos($currentSlug, $slug) === 0
318
            ) {
319
                return $currentSlug;
320
            }
321
        }
322
323
        $method = $config['uniqueSuffix'];
324
        if ($method === null) {
325
            $suffix = $this->generateSuffix($slug, $separator, $list);
326
        } elseif (is_callable($method)) {
327
            $suffix = $method($slug, $separator, $list);
328
        } else {
329
            throw new \UnexpectedValueException('Sluggable "uniqueSuffix" for ' . get_class($this->model) . ':' . $attribute . ' is not null, or a closure.');
330
        }
331
332
        return $slug . $separator . $suffix;
333
    }
334
335
    /**
336
     * Generate a unique suffix for the given slug (and list of existing, "similar" slugs.