public function filterWritablePath($path, $name = null)
    {
        if ($name === null && defined(get_called_class().'::DEFAULT_BASENAME')) {
            $name = static::DEFAULT_BASENAME;
        }

        if (!is_string($name)) {
            throw new InvalidArgumentException(
                'The target file name must be a string or use static::DEFAULT_BASENAME.'
            );
        }

        $path = $this->filterPath($path);
        $test = $this->basePath().'/'.$path;

        if (is_dir($test)) {
            if (is_writable($test)) {
                $path .= '/'.$name;
            } else {
                throw new InvalidArgumentException('The target path is not writeable.');
            }
        } elseif (is_file($test)) {
            if (!is_writable($test)) {
                throw new InvalidArgumentException('The target file is not writeable.');
            }
        } else {
            $info = pathinfo($path);
            $path = $this->filterWritablePath($info['dirname'], $info['basename']);
        }

        return $path;
    }